• Post Reply Bookmark Topic Watch Topic
  • New Topic

why does my code skip scanner input?  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys this is a small program to add contacts to a phone anyway everything seemed to be going smoothly until I ran the program,it did something unexpected for some reason it seems to skip name = scan.nextline() in addRecords() when the program runs it displays enter a name and does not give me an option to input anything instead skips straight to "1 to quit any other key to main menu" does anybody know why this code is getting skipped for some reason?

here is the code I only posted the main code because the problem seems to be in the main.java not with the other classes




thanks
 
Julian Isaac
Greenhorn
Posts: 12
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens if you just use next() instead of nextLine()?

Do you expect the name to be one token or something like first-name [middle-name] last-name?  then perhaps use next() and hasNext() in a loop.

nextLine() talks about skipping input, so perhaps it's not working the way you'd expect compared to (for e.g.) nextInt()...
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's what's happening.  Scanner has a design flaw *cough*bug*cough* where it will leave an "enter" key press (CRLF on Windows) in its buffer.  When you write you would think that nothing is in scanner's buffer, but you'd be wrong.  The CRLF is still there.  If you write another scanner.nextInt(), scanner knows to skip these characters to find the int.  However, scanner.nextLine() reads everything in the buffer up to the text end-of-line, which happens to be CRLF on Windows.  So it reads the CRLF and returns an empty String.

To fix this, just call scanner.nextLine() just before your "real" nextLine():
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:. . . .  Scanner has a design flaw *cough*bug*cough* where it will leave an "enter" key press (CRLF on Windows) in its buffer. . . .
It is neither a design flaw nor a bug. It is a poorly‑documented feature.

As we all know, the default delimiter for a Scanner is multiple whitespace, so a single space, many spaces, or a line end character all count as part of the delimiter. That means you can use the nextInt() method thrice and read any of the three inputs following with the same results:-
123 456 789
123
456


789
123      456             789
Now, we all know the ints you are trying to read are 123 456 and 789, not “123 ” etc. If you leave the following space behind because it is part of the delimiter, you are also going to leave any following line end characters behind, because they are also part of the delimiter. It says quite clearly in the method documentation that nextLine() returns
the rest of the current line, excluding any line separator at the end.
My emphasis.
That is crystal‑clear, but many people who write Java® books never seem to read it.

So after reading an int, the rest of the current line may be empty, and nextLine returns the empty String or a whitespace‑only String. There are several solutions. I think the ideal for Scanner is to create a utility class and use that utility class for all keyboard input. You can then create a loop so as to return a printing String (i.e. one which produces visible output on screen). You can call nextLine twice. You can follow next() by nextLine() as suggested (I think) by Liutauras Vilda:-There are subtle differences between those solutions.
* * * * * * * * * * * * * * *
Earlier in this post, I wrote:not “123 ” etc.
Note there is a space after the number.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question is covered in our Beginner's FAQ: https://coderanch.com/wiki/660097/java/Java-Beginners-Faq
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!