• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java, while loop, try and continue  RSS feed

 
Tomas Richardson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The continue is working. But when the call to scan.nextInt fails it does not remove the bad input from the Scanner's input. So then when you call nextInt() again you get the same bad input.

Add the following 2 lines before your continue statement.
 
Tomas Richardson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, so I figured out that I needed a scan.next(); just before the continue...

However, I have no idea what the connection is between scan.next(); and continue is.. if someone could explain I would be very grateful.
 
nick woodward
Ranch Hand
Posts: 382
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


I'm new too, but should be able to help. think you should be able to put another while loop round the try/catch block. just turn this:

"but I'd like the user to be able to keep entering input until they enter a type int"

into a condition.

i'm sure someone will be able to give you a better idea, but it should be reasonably easy. am trying it now and will let you know




 
nick woodward
Ranch Hand
Posts: 382
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use scan.next() instead of 'continue'.


that should point you in the right (sort) of direction
 
Derik Davenport
Ranch Hand
Posts: 92
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)?

 
nick woodward
Ranch Hand
Posts: 382
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah, apologies, I didn't see derik's reply! listen to him.

definitely not me!
 
Tomas Richardson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)?



I think I understand now. So if the input is bad it wouldn't prompt again until it is cleared and allowed to move forward. I will look more closely at the Scanner docs. I 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.
 
Derik Davenport
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
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?

It would respond with
you typed 'George' but I need an integer.
Number too high!


the first line happens because the scanInt() call generated an error. The call to next() has now cleared the bad token
Then your program hits the continue statement.
Then your program calls scanInt() and immediately finds '123', which is too high.
And then nextInt will be called a third time, and this time it will block waiting for more input from the keyboard.


 
Tomas Richardson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ohhhhhhhhhhhhhhh.... I didn't know it did that! i can enter a several numbers hit return and it will go through each one! Ok, makes perfect sense now.

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?
 
Derik Davenport
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i can enter a several numbers hit return and it will go through each one!
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.

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?
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.

For example, in the try statement, after line 17 you could do something like this.
while(scan.hasNext()){
scan.next());
}


This will clear all input until the end of the line. Eventually hasNext would return false. Then your processing could continue to process the value of the int you got in line 17. If line 17 fails in the exception, then you will end up in the exception handlign routine immediately and the loop i provided above will never be called.

 
Tomas Richardson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Derik Davenport
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That would be my pleasure Thomas.. I don't have the chance to hang out here on JR as much as I would like but when I do am always happy to help.
I am sure that I would be lost in Python.
It seems like the first part of using a language is learning the syntax. That usually goes quick (for me). But learning all the libraries, that always takes a long time. It doesn't help that each language handles even simple things like scanning an input stream or opening a file in totally different ways. Last month I tried to parse some input in Haskel. Oh man, was I in for a rude awakening!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

One piece of advice I give ad nauseam to newbies is: When you run into a situation like this, StopCoding (←click).

User input, in almost every language, is fiddly, so you need to spend some time thinking about what you want it do, rather than worrying about the in's and out's of Java syntax.

If you're interested, one technique is described in the UserInput page. There are three parts to it, and the third one isn't finished yet; but hopefully the first two will give you some ideas to be getting on with.

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch

I think the first thing to learning a language is learning the paradigm you are using, so Java®→object‑orientation (=OO). (I think Python is OO too.)
Usually learning the syntax is child's play compared to the paradigm.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!