• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Scanner not taking input?

 
Ranch Hand
Posts: 386
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
am a little confused with the following code if anyone minds helping.

i've a scanner called 'sc' as an instance variable, and a local variable 'name' which stores the input from sc.nextLine() at line 3.
it works fine the first pass, but not the second.

do i have to clear the scanner or something? sc.nextInt() seems to work ok without the same problems...

thanks in advance!

Nick

 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A Scanner always takes input and doesn't seem ever to return null. But it has some peculiarities which are often not well documented. The nextLine method is a particular example. What does your book say nextLine does? I keep asking that and not getting answers.

I shall blind you with science by telling you that nextLine and nextInt use different delimiters.

The problem you have is that you are calling nextSomething and following it with nextLine. You might not notice, but your method calls nextInt at the end and is then repeated, calling nextLine. That is equivalent to nextSomething followed by nextLine. You can see part of the explanation here.
I believe the correct solution is to write yourself a utility class which uses a Scanner to read from the keyboard. You can find a way to deal with the problem with nextLine here. Of course, unless you are very experienced you won't see what a dreadful mistake I made in that post. You must read the whole thread.
 
nick woodward
Ranch Hand
Posts: 386
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:A Scanner always takes input and doesn't seem ever to return null. But it has some peculiarities which are often not well documented. The nextLine method is a particular example. What does your book say nextLine does? I keep asking that and not getting answers.


I will look, but this is just me messing about - i can't remember which book I learnt about the scanner class from!


I shall blind you with science by telling you that nextLine and nextInt use different delimiters.

The problem you have is that you are calling nextSomething and following it with nextLine. You might not notice, but your method calls nextInt at the end and is then repeated, calling nextLine. That is equivalent to nextSomething followed by nextLine. You can see part of the explanation here.



because the method is called twice from main? or are you saying that the method itself calls nextLine(), nextInt(), and then nextLine() again for some reason that (you're right) I've not noticed.

thanks for the link though, will start reading it now!


I believe the correct solution is to write yourself a utility class which uses a Scanner to read from the keyboard. You can find a way to deal with the problem with nextLine here. Of course, unless you are very experienced you won't see what a dreadful mistake I made in that post. You must read the whole thread.



clearly the mistake is you being a nuisance :P

will make sure i read all of it!

Nick
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you call that method twice from the main method. It calls nextLine then nextInt, then it goes back to the beginning and calls nextLine then nextInt.
 
reply
    Bookmark Topic Watch Topic
  • New Topic