• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

why does my code skip scanner input?

 
Ranch Hand
Posts: 543
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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()...
 
Sheriff
Posts: 7111
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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():
 
Marshal
Posts: 74004
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 16591
277
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question is covered in our Beginner's FAQ: https://coderanch.com/wiki/660097/java/Java-Beginners-Faq
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you ❣️
 
Campbell Ritchie
Marshal
Posts: 74004
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I have slightly changed my opinion about how to use nextLine() in a utility class. I now use something like this:-The strip() method was introduced in Java12 Java11; for older versions try the trim() method instead. The Unicode escapes are posh quote marks.

I took great delight in being naughty and writing a while loop with a semicolon at its end


[edit[Changed Java12 to Java11
 
Rancher
Posts: 157
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Although I don't have any intensions to hijack this thread with an argue about scanner, I see "my two cent" fit here:
Scanner itself isn't the issue and neither is its may not-so-optimal documentation but the way it's taught.
Back when I first got a book about Java in my hands the "default" line I learned from it was BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(System.in)); combined with anything read as whole lines and parsed by individual Integer.parseInt(String).
I only learned about Scanner when someone explained to me why to prefer ArrayList over Vector and StringBuilder over StringBuffer. The book was about 1.3 - so even before the release of JavaSE1.4.
If Scanner and its methods were taught so that beginners can fully grasp its pitfalls we sure would get this qustion less frequently - but it's about the same with all those rather wrong JavaMail code that's re-used over again cause poeple still don't get the difference between smtp and smtps and hence keep setting SSLSocket for smtp cause they keep using TCP/465: The internet never forgets!
 
Campbell Ritchie
Marshal
Posts: 74004
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Bendford wrote:. . . The book was about 1.3 - so even before the release of JavaSE1.4.

And before the release of Scanner, so either buffered reader or JOptionPane were the standard means for getting keyboard input.

If Scanner . . . were taught so that beginners can fully grasp its pitfalls . . .

I have a Java1.3 book, too; when I used it I was taught that many people wrote their own keyboard input classes; I did, using buffered readers, and it is typical beginner's code. When Scanner came out we were told that it was no longer necessary to write one's own utility, but I have since realised that such a utility is useful. And it uses the power of Scanner; it will return an int without throwing exceptions if the wrong input is given.
What chance have teachers got if they read books saying that nextLine() returns the next line? Which the documentation (link in 2017 post) distinctly denies.
 
reply
    Bookmark Topic Watch Topic
  • New Topic