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!
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.
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.
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:
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.
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.)
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.
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.
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.
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.
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.
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.
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.
Post by:autobot
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