• 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

Getting a keyboard key to exit a loop

 
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
did this a long time ago cant remember

trying to just have a loop exit when the player presses say "f" or "F"

heres my code



this line doesnt seem to be working

while (!userInput.equals("f"))

any ideas?
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't do anything within your while loop. Other than printing "ok" over and over again.
Your program needs to scan for new inputs within the while loop.
The boolean check !userInput.equals("f") will always check the first input you got from line 8 when calling the method .next().



 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you need is nextChar() but Scanner doesn't have one. You could try calling
after creating the Scanner object. This works for String.split() but I don't actually know if it would work in your case. Give it a try.

Failing that, you could try scan.nextByte().
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

you could try scan.nextByte().


Try a test program with that.  It might not do what you want.  
 
jon ninpoja
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't do anything within your while loop. Other than printing "ok" over and over again.
Your program needs to scan for new inputs within the while loop...

I know this... The point is the conditional isn't working its looping OK regardless of what key is pressed.
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jon ninpoja wrote:I know this... The point is the conditional isn't working its looping OK regardless of what key is pressed.


It supposed to loop OK whenever input ins't "f".

But seems you are missing a logical understanding of this routine. The point is, that once you enter the loop, you have no way to exit it. And now the question to you. Do you understand why? Hint: how many times you ask user for an input?
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:. . .  you could try scan.nextByte().

I don't think that will work; t would expect input like −128 or 127 or all stations between.

I think the reason there isn't a nextChar() method is that Scanner is intended to operate on tokens and you don't usually have a token comprising a single char. If you have a token which isn't anything else, it is a String, even when it is a 1‑length String. Yes, you can try changing the delimiter, but I think that is overkill. I don't think there is any risk of getting an empty String before the start of the input, at least. If I want a char, I would try myScanner.next().charAt(0). There are two other ways to do it that I can think of just at the moment. One is to shorten the input to a one‑letter String with substring(). The other, which I think I would prefer, is to look for methods to tell whether a String starts with or ends with something.
 
jon ninpoja
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so i tried this approach,



but it just keeps looping,wont exit the loop after pressing f

somebody please help me troubleshoot this,dont give me the code or the answer just point me in the right direction
i can do this with ints easy,just not when using strings and scanner methods

 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The next() method returns a value that the code is not saving for testing.
The Scanner class object: userInput will never equal a String ("f")
 
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Scanners are not variables. It will only read in this case.

if (userInput.equals("f")) {
               fPressed = true;
           }

This will never trigger due to userInput being Scanner type.
 
jon ninpoja
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks norm am i on the right path? would a do-while loop not be better for this situation,or doesnt it matter?
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you fix the code and get it to work?

Also try writing it as a do{}while and see how that works.
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jon ninpoja wrote:thanks norm am i on the right path? would a do-while loop not be better for this situation,or doesnt it matter?


You didn't get what Norm was telling you.

The line 1 you see simply takes an input from the user and discards it, or more precisely doesn't save.
What you probably want to do, is to assign the returned value in line 1 to some variable and then this variable check within the if statement similarly as you do now.

i.e.
String enteredByUser = userInput.next();
 
Jesse Schlicklin
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:

jon ninpoja wrote:thanks norm am i on the right path? would a do-while loop not be better for this situation,or doesnt it matter?


You didn't get what Norm was telling you.

The line 1 you see simply takes an input from the user and discards it, or more precisely doesn't save.
What you probably want to do, is to assign the returned value in line 1 to some variable and then this variable check within the if statement similarly as you do now.

i.e.
String enteredByUser = userInput.next();



Once something is read in then it goes straight to the garbage collector unless its placed somewhere - usually a variable.
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Schlicklin wrote:Once something is read in then it goes straight to the garbage collector unless its placed somewhere


Well, not to mislead those who take the notes, it doesn't go straight to garbage collector, instead, objects become eligible to be garbage collected once they go out of scope or have no active references via which object could be accessed. When that actually happens user doesn't know. And probably don't need to.
 
jon ninpoja
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok so finally got it working...thanks for all your help!!! learned a lot!!!

 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At line 18, instead of having break, better have:
fPressed = true;

That way you'll exit loop, and that is a cleaner way of doing this. Otherwise you don't need even variable fPressed so you could have while(true) (<-- don't do that).
 
jon ninpoja
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok,see what you mean....

when is break used then? or is it avoided as much as possible?

thanks for all your help!!!
reply
    Bookmark Topic Watch Topic
  • New Topic