• Post Reply Bookmark Topic Watch Topic
  • New Topic

Evaluating user input for String or int  RSS feed

 
Ursula Coyote
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ok, I'm beating my head against the wall with this one.

I have to take a users input. The general gist of the problem is I want to convert a decimal number entered by the user inputs into it's binary equivalent....the conversion part I know how to do.

I'm stuck at the user input phase. If the user inputs a decimal number within the correct range (lets say between 0-5000 for this example) the conversion goes ahead as planned and the program outputs on the screen the binary equivalent.

If on the other hand the user inputs a number outside of this range ... OR a String OR and empty space, an error message is given and the user is asked to try again. I don't know how to handle it if the user enters the different types...int or String.

Can someone give me some suggestions please?
 
Carey Brown
Saloon Keeper
Posts: 3314
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this...
 
Ramy Nady
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ,

1- You need to use Scanner class and nextInt() to get the input integer.
2- You can use Integer.ParseInt() to parse this number.
If it throws NumberFormatException you are sure that the String does not contain a parsable int (So you will display your error message)
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ursula Coyote wrote:Can someone give me some suggestions please?

Yes. Read the UserInput page. There are three parts to it, but you should get enough out of the first one to do what you want.

I warn you though: it's not short - and that's because user input is not simple.

Just take this one "easy" task. You have you:
(a) Display some sort of prompt.
(b) Read in what your user typed.
(c) Convert it to a double (AND deal with any problems you encounter while doing so).
(d) Make sure that the number they typed is within a certain range.
and you have to do it consistently.

Also (and not many books will tell you this):
(e) You have to deal with the fact that your "user" may be a complete moron who doesn't follow instructions.

So your code not only needs to do what it's supposed to; it needs to be FOOL-proof.

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And I am the FOOL Winston is trying to proof you against.

The following only works if you get input from the keyboard. It can go horribly wrong if you are reading from a text fileRob Spoor taught me how to do that. It will never throw an Exception.
I suggest you put such a method in a utiity class; that way you make it avalable to all code and you only ever need one Scanner object.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!