• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regrading the Scanner object  RSS feed

 
Ashwin Rao
Ranch Hand
Posts: 89
C++ Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the basic structure of my program till now:


On line 23: Why doesn't the 'choice' work with existing Scanner object?
If I remove line 23 then the program doesn't wait for my input.
Any help is much appreciated!
Thanks in advance!!
 
Dave Tolls
Ranch Foreman
Posts: 3064
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is down to the fact that nextLine() works differently to the other next() methods.
If you only want a single "word" (ie no spaces in the input) then use next(), otherwise you'll need to make two calls to nextLine().

The reason is that the regular nextXXX() methods read up to the next token (in the default Scanner that is a whitespace character, so space, tab, new line etc), they do no, however, read that character, so it is still in the buffer.

So reading in:
1
with nextInt() will leave a new line in the buffer.

Now, nextLine() reads in up to and including the next new line character (it discards the new line). So, after the above nextInt, if I then do a nextLine() it will read in that new line that has been left in the buffer, and return an empty String.
 
Ashwin Rao
Ranch Hand
Posts: 89
C++ Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whoa! Mind blowing!! Thanks a lot!!
 
Dave Tolls
Ranch Foreman
Posts: 3064
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashwin Rao wrote:Whoa! Mind blowing!! Thanks a lot!!


I ought to keep that one in my past buffer, to be honest.

It comes up a lot, which (to me) implies a problem with how Scanner was designed.
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In fact, in comes up so often we have a couple of FAQs that address that very issue. Here and here.
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:This is down to the fact that nextLine() works differently to the other next() methods. . . .
Or that people are not taught about it. I have yet to see a book with a good explanation of nextLine().
 
Dave Tolls
Ranch Foreman
Posts: 3064
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Dave Tolls wrote:This is down to the fact that nextLine() works differently to the other next() methods. . . .
Or that people are not taught about it. I have yet to see a book with a good explanation of nextLine().


I have yet to see a decent explanation of Scanner as a whole.
It just looks (to me) like it's been cobbled together.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!