Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Breaking out of two loops

 
Steven Alvarez
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I have the following code:



My question is how can I break out of two loops. Because when you run this progam is asks someone to guess a number from 1 to 1000. Once they guess correct, it asks for then to type "yes" to continuue, and "no" to stop.

Currently when they say no, it breaks out of the inner loop and asks them to guess again. When it's suppose to end the program.

Then I tried:


But that doesn't work either. Thanks for any help.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, while(true) doesn't provide any condition for gracefully exiting the loop. Ideally, you should use a condition that considers the user's input.

If that's not possible, then using break with a label is also an option.
[ October 18, 2008: Message edited by: marc weber ]
 
Steven Alvarez
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get the same thing with this :-/
here is the code:


 
Steven Alvarez
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it works fine for typing END however

I also tried while (loop1 = true) and while (loop2 = true)
[ October 18, 2008: Message edited by: Steven Alvarez ]
 
Steven Alvarez
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anyone know how to fix it?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The loop's condition is checked only before beginning each iteration. If the condition changes to false somewhere within the loop's body, it won't "know" that until it finishes that iteration and checks to see if it should iterate again.

So you need to either restructure your code so that the condition is changed at the bottom of the loop, or you'll need to use a "continue" to get out of the current iteration or "break" to get out of the loop entirely. (Note that if you break out entirely, then your "while" condition might not serve any purpose.)

Also...

I don't think it's working fine when the user enters "end." I think it's throwing an exception when your code tries to parse the String "end" as an int. So it's ending, but not gracefully. The reason it tries to parse is because your String comparison is failing.

You shouldn't compare Strings using the == operator. Use String's equals method instead. For example...

if("end".equals(userAnswer))...
[ October 18, 2008: Message edited by: marc weber ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(As a side note, do you realize that you're creating a brand new Scanner object with every iteration of the inner loop?)
 
Don Solomon
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
throw that mess away. it is obviously case logic which is still messy. implement it using the state pattern; your code will be clean, easy to maintain, and something you can be proud of.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Don Solomon:
...implement it using the state pattern...

That might be jumping in a little deep for this beginner's forum. What do you say we get the fundamentals of looping down first?
 
Campbell Ritchie
Sheriff
Posts: 51447
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And never write == true, because of the possible confusion with = true.
Writing if (xxx = true) is a serious logic error because you are actually changin gthe value of xxx in the "if". The same applies to a while.

You write if (xxx).
 
Campbell Ritchie
Sheriff
Posts: 51447
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two other examples about == and = in these two threads: 1 2.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic