• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Condition: Integers only

 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
r phipps
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could just use the ABS method to return the absolute value of the interger.
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic