• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

cant seem to end while loop

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.






 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ben Hultin
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic