Tomas Richardson wrote:Hi everyone. I'm fairly new to java - coming from an python hobbyist background. Starting university next year and wanted to get a head start in programming with java. So, I'm just recreating some simple projects I used to learn python but converting them to java. This program is a simple guessing game 1-100 and the output will let the user know if their guess is to high or low so that they can get closer to the answer.
I'm slowly building on the program. The basic idea works fine.. but then I decided to add a try block to catch an error from the user input (i.E if they enter a string rather than an int type). The code works perfectly if I replace my continue statement with a break statement.. but I'd like the user to be able to keep entering input until they enter a type int, rather than breaking them out of the loop/program. In python I would normally use a continue to pop them back up to the start of the while loop which will keep prompting them for input. However, in java it seems that the continue is just popping the code to the println above it and not to the start of the loop so what I get is an infinite loop with a message 'invade int type' instead. What am I not seeing?
Thank you in advance for your time.
Derik Davenport wrote:Tom,
there are lots of interlaced replies here. You might have missed mine above.
Scaner.next() has nothing with continue.
Your understanding of continue is correct. It is doing what you think. It is your understanding of the Scanner class that is incomplete.
When the exception occurs during the call to nextInt(), the bad token is not removed. We say "the scanner does not advance". scaner.next() is clearing out the old input, forcing the scanner to advance past the bad input we know is there.
As a test of your understanding, using the 2 lines I suggested above, what would happen if you entered "George 123" (without the quotes)?
No, I mean if you typed the program to include the 2 lines I suggested above, and your ran it, and used "George 123" as your input, how would your program respond?
would assume (and i will test it). without the quotes I would assume the complier would throw a fit because it is expecting a string inside quotes and stop there.
Yep. Because the nextInt() parses as many digits as it can until it hits the "delimiter". YOu can set the delimiter to be anything you want, but in this case it is set to white space. Each time the scanner comes across a space, it stops reading, creates the integer out of what it has, and then advances to the point after the space. When nextInt is called again it continues where the last call left off.
i can enter a several numbers hit return and it will go through each one!
There is nothing you can do in a simple program like this that will force the user to stop typing after just 1 or 2 digits. But what you could do, is clear the input stream after the first number is found.
What if I only wanted to user to enter just one thing, rather than a bunch? for example just 20, rather than 1 2 3 40 59?
Tomas Richardson wrote:Thank you so much for explaining it to me Derik, seems much different than the python's raw_input way of getting input. It's a nice feeling to walk away from a thread having a very clear idea of how it is working and why it needs to be a certain way. Again, very much appreciated.