Forums Register Login

My program isn't requesting user input outside of my while loop and I don't know why.

+Pie Number of slices to send: Send


As you can see in the code, I've put the request for user input in bold that isn't working. When I put this line inside of the while loop, it works, and I can't figure out why it doesn't work outside of the loop. Thanks for any help!
+Pie Number of slices to send: Send
You really need too clean up your indentation. Here's a piece of your code with proper indentation:

Getting back to your question. What input are you typing in and what response are you getting? Post a copy of your console if you can.

Looks like the program should just exit when the last nextLine() is satisfied.
1
+Pie Number of slices to send: Send
Well, why do you think it should work? If you can answer that question, then what you think is obviously not true. With that knowledge, you'll be closer to answering your own question.
+Pie Number of slices to send: Send
Looks like when you call

that a new-line ("\n") is left pending in the input stream and that as soon as you call nextLine() it acts on the pending new-line as though you typed it in.
Not being a big Scanner fan, perhaps someone else can comment on this.
+Pie Number of slices to send: Send
It looks like you've run into a bug design flaw of Scanner.  Quick answer: put another inputUser.nextLine() just before the bold line and ignore the result.

Why it works: the Scanner#nextInt() method will "consume" the input up to the newline that the user enters.  When there's a nextInt() after it, it will skip the newline character, but nextLine() looks for the newline character the end the line.  Since there's still a newline character in the "pipeline", it consumes that and ignores the other input.  So you need two nextLine()'s after a nextInt(), or this code:
+Pie Number of slices to send: Send
If you follow nextInt() by a nextLine(), that should clear the pending new-line. You'll have to do that every place that you call nextInt().

Edit: Knute's answer is better.
+Pie Number of slices to send: Send
 

Carey Brown wrote:You really need too clean up your indentation. Here's a piece of your code with proper indentation:

Getting back to your question. What input are you typing in and what response are you getting? Post a copy of your console if you can.

Looks like the program should just exit when the last nextLine() is satisfied.



Learning the grammar of a new language can be a pain (there's always a way to* improve ;) ).
In all seriousness, thank you for the response. My console looks like this:


After this, I cannot type anything into the console. However, if I move my request for user input into the While Loop at the very bottom, it does request more input. The intention is to ask the user if they would like to start the program again.
+Pie Number of slices to send: Send
 

Knute Snortum wrote:It looks like you've run into a bug design flaw of Scanner.  Quick answer: put another inputUser.nextLine() just before the bold line and ignore the result.

Why it works: the Scanner#nextInt() method will "consume" the input up to the newline that the user enters.  When there's a nextInt() after it, it will skip the newline character, but nextLine() looks for the newline character the end the line.  Since there's still a newline character in the "pipeline", it consumes that and ignores the other input.  So you need two nextLine()'s after a nextInt(), or this code:



Thank for the reply, I have tried using String line = inputUser.next().nextLine();  ... but I am getting the error "The method nextLine() is undefined for the type String"  (sorry for double posting but I don't know how to edit my post.)
1
+Pie Number of slices to send: Send
Well, it looks like that code isn't working.  Try just a inputUser.nextLine() before the "bold" line.
+Pie Number of slices to send: Send
 

Knute Snortum wrote:. . .

So when the input looks like

123
Knute Snortum

will that return “Knute” or “Snortum” or “Knute Snortum”?
+Pie Number of slices to send: Send
Let's look at your code with the while-loop exiting immediately:

Why would you think it would re-enter the loop after exiting it the first time? You read a line, but you ignore the result and there are no more statements to execute after that.
+Pie Number of slices to send: Send
 

Knute Snortum wishes he never wrote:


This code is wrong on several levels.  Forget I ever wrote it!
+Pie Number of slices to send: Send
 

Knute Snortum wrote:

Knute Snortum wishes he never wrote:


This code is wrong on several levels.  Forget I ever wrote it!


Didn't read full thread, but if your intention was what I think, then that bit would work I think with slight mend:
+Pie Number of slices to send: Send
 

Carey Brown wrote:Looks like when you call

that a new-line ("\n") is left pending in the input stream and that as soon as you call nextLine() it acts on the pending new-line as though you typed it in.


This is the correct explanation.

Experiment with the following:

Run the code with different combinations of line 44 active or commented out and line 48 where it is and moved after the brace on line 49.
+Pie Number of slices to send: Send
This is why I think Scanner is often not a good choice.
As soon as you need to read in a line (ie something that is more than one toke, like someone's name) as well as numbers, you end up having to jump through hoops to get around the fact that that isn't what it was designed for.

Just read in the lines and then parse them.
+Pie Number of slices to send: Send
 

Dave Tolls wrote:. . . Just read in the lines and then parse them.

That isn't what Scanner was designed for either. If you are going to parse things, use a buffered reader and pass the whole line to something else. The real problem is that Scanner is not well documented. And I would be interested to see what OP's book says nextLine does. I don't remember seeing a book which gets that bit right.
+Pie Number of slices to send: Send
Just new to Java in general myself. I agree "Scanner is poorly documented"  I had a similar issue with a server app for one of my class's except I read from a file noy user input.  Turns out it was how scanner interprets Scanner.nextLine() and Scanner.nextInt().  After you use Scanner.nextInt() the cursor remains on the same currrent input line.
+Pie Number of slices to send: Send
In defence of Scanner, nextLine() does say it returns the remainder of the current line. Maybe it is better to say it is poorly‑named. But that pitfall catches everybody out.
+Pie Number of slices to send: Send
 

Campbell Ritchie wrote:

Dave Tolls wrote:. . . Just read in the lines and then parse them.

That isn't what Scanner was designed for either. If you are going to parse things, use a buffered reader and pass the whole line to something else. The real problem is that Scanner is not well documented. And I would be interested to see what OP's book says nextLine does. I don't remember seeing a book which gets that bit right.



That was (at least in my head) what I was suggesting.
As soon as you have something other than a fairly well structured set of inputs that you can supply Scanner with a reasonable regex then you're best served by using a bog standard BufferedReader...eg, just read lines.
This one time, at bandcamp, I had relations with a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 678 times.
Similar Threads
Calling methods using dynamic binding
quick beginners question
its a classic! help with area of shapes!
Can't find my error
storing user defined variables in an array
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 07:43:24.