• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting user input  RSS feed

 
Janis Mittelstaedt
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been playing around with simple java programs lately to see what I can and can't do.
In my newest program I asked for user input. The program is rather simple so the user enters the information into the command prompt. For this I'm using the Scanner.
The problem I'm now facing is that I want the program to keep running and keep asking the user for the right input even if the user enters something wrong (a string when a number should be given).
This is the code of the method which gets user input:


I couldn't think of any other way of doing this. I was sure it would work, but for some reason after getting an input that is not a number instead of asking for input again, it just keeps spamming "Please enter a number: ". Seemingly skipping the input.next().
I'm probably going at this the wrong way. Could one of you help me please?
 
Paweł Baczyński
Bartender
Posts: 2086
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I won't recommend this kind of recurrency by calling the method again from catch block.
Instead consider using a loop and a hasNextInt() method of Scanner class.

And it keeps spamming "Please enter a number: " because nothing changed the state of input.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so...what happens if your code throws an exception? You call the same method again.

Instead, you should print out the contents of the exception (for starters) so that you KNOW that an exception has been thrown.
 
Janis Mittelstaedt
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:so...what happens if your code throws an exception? You call the same method again.

Instead, you should print out the contents of the exception (for starters) so that you KNOW that an exception has been thrown.


Well, I didn't want it to be printed out. The user doesn't need to see that. He just needs to know that he should enter a number and not something else.
I could of course print out something like "That was not a number, please enter a number: " into the catch block, but what if the user writes something else again?

Pawel Pawlowicz wrote:I won't recommend this kind of recurrency by calling the method again from catch block.
Instead consider using a loop and a hasNextInt() method of Scanner class.

And it keeps spamming "Please enter a number: " because nothing changed the state of input.


Could you explain how exactly this would be done? And what do you mean by 'state of input'?
 
Paweł Baczyński
Bartender
Posts: 2086
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Janis Mittelstaedt wrote:
Pawel Pawlowicz wrote:I won't recommend this kind of recurrency by calling the method again from catch block.
Instead consider using a loop and a hasNextInt() method of Scanner class.

And it keeps spamming "Please enter a number: " because nothing changed the state of input.


Could you explain how exactly this would be done? And what do you mean by 'state of input'?


Something like:

In your example you do nothing to the input object, so no matter how many times you call genNumber() it will result in the Exception being thrown.
You should try to read user input again within the loop.
 
Janis Mittelstaedt
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So after a lot of testing I finally got it working. Thanks for the help!

Although I still don't quite understand how the 'nextInt()' method works. Why does it wait for user input the first time it's called and then never again until it gets an Integer? What exactly does the method actually do?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Janis Mittelstaedt wrote:
Although I still don't quite understand how the 'nextInt()' method works. Why does it wait for user input the first time it's called and then never again until it gets an Integer? What exactly does the method actually do?


Can you explain what you mean by "wait for user input the first time it's called and then never again until it gets an Integer"?

Henry
 
Janis Mittelstaedt
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, let me give you an example of how the program worked using the code from my first post:

Please enter a number: randomText
Please enter a number: Please enter a number: Please enter a number: Please enter a number: Please enter
a number Please enter a number: Please enter a number: Please enter a number: Please enter a number: Pl
ease enter a number: Please enter a number: Please enter a number: Please enter a number: Please enter a


And so on until it crashes.

I finally got the code working by removing the 'nextInt' method and using the 'next()' method instead, then checking if the String can be parsed to an Integer like so (I also used a while loop instead of a recursive method):



This code works. I can write letters again and again and the program won't proceed until I give it an Integer:
Please enter a number: oh
Please enter a number: this
Please enter a number: is
Please enter a number: text
Please enter a number: 5


So my question basically was: Why does the program crash when using 'nextInt()'? When calling the next() method it will ask for user input again and again while calling the nextInt() method seemed to only ask the user once.
I hope I'm making sense - English is not my first language
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Janis Mittelstaedt wrote:
So my question basically was: Why does the program crash when using 'nextInt()'? When calling the next() method it will ask for user input again and again while calling the nextInt() method seemed to only ask the user once.
I hope I'm making sense - English is not my first language



Basically, your new updated code calls next() to get a token, and then tries to convert that to an int. If not successful, it gets the next token, and tries again.

The nextInt() method, is almost like that -- it also gets a token and tries to convert it to an int. There is a difference however. If it is not successful, it doesn't consume the token -- this is so that your error handling code can read the token to take corrective measures. Unfortunately, your program simply calls nextInt() again. And since, the token is not consumed, it will try again with the same bad data and get the same error again. And again, And again. etc.

In summary, it doesn't go back to ask the user for more input, because it hasn't consumed the old input yet. It is still processing it.

Henry



 
Janis Mittelstaedt
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for that explanation, it really helped!
 
Paweł Baczyński
Bartender
Posts: 2086
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also do this using hasNextInt() instead of InputMismatchException

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!