0
While loops can be weird sometimes...
In my most recent code: https://code.sololearn.com/cn45WGOEuNus/?ref=app The variable "decision" needed to be asked twice because the while loop still had the function <enter> left in the queue making the first "decision=s.nextLine()" capture the enter. The queue for reference looked like this: <1> <2><enter> but since the while loop is continuous it is able to catch that last enter. My question is if anyone has a solution that, in the case the user doesnt have this problem and has to type "no" twice, will circumvent this?
5 Antworten
+ 1
if you don't want to write nextLine() twice what you can do instead is
in place of guess=s.nextInt(); write guess = Interger.parseInt(s.nextLine());
+ 1
enter will be discarded automatically when you call parseInt after taking string as input.
+ 1
no it's not that.
It didn't work because when you enter some integer and press enter input buffer gets loded with your int value and new line character and when you do nextInt if will fetch only int that you have entered the new line character still stays in the buffer so when you call next line it will first fetch new line character from buffer go for the next line on the second call of next line();
0
Tanay Does this makeit so that the only value acceptable here is a int value making the <enter> not possible here?
0
Tanay It didnt work due to the Interger not being noticed as a type