aspose file tools*
The moose likes Beginning Java and the fly likes Condition:  Integers only Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Condition:  Integers only" Watch "Condition:  Integers only" New topic
Author

Condition: Integers only

Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
I note you can use the following:

if (input.compareToIgnoreCase("Exit")==0)

when you want to say ignore whether the input is upper or lower case.

Is there something similar available when you want to ignore anything accept positive integers?
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
The only way I know to do it is to parse the string to an int and compare for greater than zero. Of course, you'd have to deal with an IllegalArgumentException in case the String is not parsable to an int...


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
r phipps
Ranch Hand

Joined: Sep 14, 2004
Posts: 60
You could just use the ABS method to return the absolute value of the interger.
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
But if you do that you accept negative values. In other words, if the user is supposed to enter the number of widgets on shelf 5 of the factory storeroom, and enters "-5", do you really want your program to translate that to "5"?
r phipps
Ranch Hand

Joined: Sep 14, 2004
Posts: 60
Well if you just want them to enter a postive value, which i dont know why they would be entering negtive anyway, you could check the integer for being < 0, if so loop back around to the input and send a message to enter postive only.
 
jQuery in Action, 2nd edition
 
subject: Condition: Integers only