[Logo]
Forums Register Login
Getting a keyboard key to exit a loop
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?
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().



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().
 

you could try scan.nextByte().


Try a test program with that.  It might not do what you want. 
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.
 

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?
 

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.
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

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")
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.
thanks norm am i on the right path? would a do-while loop not be better for this situation,or doesnt it matter?
Did you fix the code and get it to work?

Also try writing it as a do{}while and see how that works.
(1 like)
 

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();
 

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.
 

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.
ok so finally got it working...thanks for all your help!!! learned a lot!!!

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).
ok,see what you mean....

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

thanks for all your help!!!

This thread has been viewed 268 times.

All times above are in ranch (not your local) time.
The current ranch time is
Sep 21, 2018 04:12:23.