• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Rykurex Peters
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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!
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rykurex Peters
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rykurex Peters
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it looks like that code isn't working.  Try just a inputUser.nextLine() before the "bold" line.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:. . .
So when the input looks like
123
Knute Snortum
will that return “Knute” or “Snortum” or “Knute Snortum”?
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wishes he never wrote:

This code is wrong on several levels.  Forget I ever wrote it!
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dwayne Barsotta
Ranch Hand
Posts: 86
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!