This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Breaking out of two loops Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Breaking out of two loops" Watch "Breaking out of two loops" New topic
Author

Breaking out of two loops

Steven Alvarez
Ranch Hand

Joined: Nov 01, 2006
Posts: 66
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

Joined: Aug 31, 2004
Posts: 11343

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Steven Alvarez
Ranch Hand

Joined: Nov 01, 2006
Posts: 66
I get the same thing with this :-/
here is the code:


Steven Alvarez
Ranch Hand

Joined: Nov 01, 2006
Posts: 66
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

Joined: Nov 01, 2006
Posts: 66
anyone know how to fix it?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Aug 31, 2004
Posts: 11343

(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

Joined: Jul 20, 2008
Posts: 48
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.


Software development is an exercise in thinking not coding.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Oct 13, 2005
Posts: 37941
    
  22
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

Joined: Oct 13, 2005
Posts: 37941
    
  22
Two other examples about == and = in these two threads: 1 2.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Breaking out of two loops
 
Similar Threads
Can't Break Out of a Loop
Help with number guessing game
help needed with my while loop
Trouble with Guessing Game
Need help ending number guessing game and asking if user wants to play again