Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Problem with scanner.nextLine() in program

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I was making this program and I was wondering if anyone knew why when I run it the inputting of the sentence is either skipped completely or it doesn't let me type in the sentence and end there.
Here's the code:



Also I'll attach a picture of the errors I was getting.
Screen-Shot-2020-02-12-at-7.15.30-AM.png
[Thumbnail for Screen-Shot-2020-02-12-at-7.15.30-AM.png]
Screen-Shot-2020-02-12-at-7.15.42-AM.png
[Thumbnail for Screen-Shot-2020-02-12-at-7.15.42-AM.png]
 
Marshal
Posts: 67533
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hanna Roberts wrote:. . . if anyone knew why when I run it the inputting of the sentence is either skipped completely or it doesn't let me type in the sentence and end there. . . .

Yes. But please tell me what you think nextLine() does.

Watch this space: explanation and possible solution to follow in a few minutes.
 
Campbell Ritchie
Marshal
Posts: 67533
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everybody thinks nextLine() reads the next line, but it doesn't. I had to find this out the hard way myself. If you look at the documentation it tells you that you don't read the next line, but go on to it:-

That API link wrote:. . . Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line. . . .

The last two words in that quote box explain why the method is named nextLine(). One possible solution is suggested in this old post of mine. Simply call nextLine() twice.

I think it is better to write a utility class which can be used forever.You have several options about line 25, which removes whitespace from the text. If you miss out strip(), you might get an input all whitespace: “      ”. The strip() method was introduced in Java11; for earlier versions try the trim() method.

[edit 1]Readers are welcome to use that code, but please everybody attribute its origin.
[edit 2] You may do well to make the nextLine() method take a String parameter and insert the folllowing between lines 21 and 22: System.out.print(message); The message would be something like, “Please enter full name: ”
 
Saloon Keeper
Posts: 6796
64
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way to describe the problem is that nextInt() will leave a pending new-line in the input stream and when nextLine() is called it sees that as though someone entered an empty string. So to fix the problem you need to flush the pending new-line before you call your desired nextLine(). It's a little confusing because to flush the  new-line you also use a call to nextLine(). So you end up with:
 
Hanna Roberts
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you’re saying any time I use nextLine() after a nextInt()  I will always have to flush it, so I write it twice?
 
Carey Brown
Saloon Keeper
Posts: 6796
64
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hanna Roberts wrote:So you’re saying any time I use nextLine() after a nextInt()  I will always have to flush it, so I write it twice?


Yes. This includes nextInt(), nextDouble(), etc., anything OTHER THAN nextLine().
 
Hanna Roberts
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:

Hanna Roberts wrote:So you’re saying any time I use nextLine() after a nextInt()  I will always have to flush it, so I write it twice?


Yes. This includes nextInt(), nextDouble(), etc., anything OTHER THAN nextLine().



Oh ok. Thank you!
 
Campbell Ritchie
Marshal
Posts: 67533
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hanna Roberts wrote:So you’re saying any time I use nextLine() after a nextInt()  I will always have to flush it, so I write it twice?

Yes, unless you have input like this, where the prompts are shown in coloured text:-

Please enter age: 27 Campbell Ritchie
Please enter name:

Second version in case anybody has difficulty reading red text:-

Please enter age: 27 Campbell Ritchie
Please enter name:

Believe it or not, a Scanner will get the name out of that input with nextInt() followed by nextLine(), but it might record the name as “ Campbell Ritchie”. That is the advantage of the utility class: it contains all the awkward code, so you don't have to think about it again.
 
Replace the word "snake" with "danger noodle" in all tiny ads.
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!