File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes cant seem to end while loop 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 "cant seem to end while loop" Watch "cant seem to end while loop" New topic
Author

cant seem to end while loop

Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
I am working on a while loop that will continue until the user types in "end turn". The problem is when I try this, it just ignores the conditions and repeats the loop two more times before it stops and waits for another input.



System.out.print(currentPlayer + ", would you like to roll again? (\"roll\" or \"end turn\" or \"end game\")");

This is what gets printed twice after I prompt to end turn. The funny thing is, the first time it doesnt stop and wait for a prompt of what to do next, it just ignores:

userInput = stdIn.next();

and runs through the loop again, before stopping at the prompt after printing out the message again. I must have the while condition set up wrong, because it seems to ignore the "end turn" check.

I appreciate any help in the matter
Thanks a lot
Lee Kian Giap
Ranch Hand

Joined: Jan 23, 2008
Posts: 213
Hi,

I guess you are using Scanner class , am I right ?

You have two problem here
1) the word "end turn" read as 2 input String into your system
2) the while condition have problem , are you trying to use && instead of ||


If you don't want to use Scanner class, you might use the following code to read user input

BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput = stdIn.readLine();



I think this will help you , please sort out what really you want to achieve with the condition in your while loop.







SCJP 6, SCWCD 5, SCBCD 5
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Translating your condition into English, it says something like

Do the contents of the loop as long as one of the following is true:
* userInput isn't "end turn"
* userInput isn't "end game"

If you think about it, one of those things must ALWAYS be true. If one of them becomes false, then by definition the other one must be true, since a String can't be equal to two other different Srings at once. The loop will never end! If you use an && instead of an ||, you'll be saying

Do the contents of the loop as long as either of the following is true:
* userInput isn't "end turn"
* userInput isn't "end game"

As soon as one becomes false, the loop stops.

[Jess in Action][AskingGoodQuestions]
Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
I never thought about it, ofcourse its going to just repeat itself, one of them will always be true with an || logical operator. Thanks a lot for clearing up the process for me.
Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
well I tried changing from || to && and I still get the same result. So maybe it does have to do with the Scanner class Lee Kian Giap was talking about. So what your saying is Scanner class has a problem accepting text strings with white spaces in them? So by adding a underscore into the end turn as so: end_turn would fix the problem?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Yes, I only tried to clarify the point about the logic; the stuff about Scanner holds as well. It's not that Scanner "doesn't read Strings with spaces", it's more that the method you're using is designed to return a run of non-space characters. Try using "nextLine()" to read full lines of output.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: cant seem to end while loop
 
Similar Threads
illegal start of an expression
help needed with my while loop
calling a method w/out an object
variables may not be initialized
Problem getting Logical operator to work