• Post Reply Bookmark Topic Watch Topic
  • New Topic

Scanner problems?  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys I am having problems with the scanner getting input,the problem I have is that it seems to skip the nextLine part so I can't actually enter anything in so for example in my program if I choose add Country when add country appears in the console it does not give me an option to enter input in instead just skips to System.out.println("enter choice") again,

somebody told me to use the Scanner a global static variable instead of declaring a new Scanner object in every method but the thing is since I started using the Scanner as a global object this problem started to occur when I declare a scanner object in each class this does not seem to happen and everything works fine,

why is this the case and how do I get around this problem?

thanks I'll post the c ode below

 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way the Scanner class works is confusing IMO.  Here's what's happening:

* You call scanner.nextInt().  Scanner will "consume" the part of the buffer that is an integer.  When you enter this from the keyboard, the <enter> leaves (depending on the OS) a CR, LF, or CRLF at the end.

* If you call scanner.nextInt() again, it knows to move past the <enter> and consume the integer.  There is still an <enter> at the end of the buffer.

* Now you call scanner.nextLine().  All nextLine() knows is to consume the buffer up to the end-of-line, which looks just like the <enter> character(s).  But you already have an <enter> in the buffer, so it consumers this and returns an empty String without you entering anything.

The easiest way around the problem is to call nextLine() twice, once to consume the <enter> at the end and once to get your actual input.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry guys just noticed I asked the same question a few days ago,sorry my bad
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:The way the Scanner class works is confusing IMO.  Here's what's happening:

* You call scanner.nextInt().  Scanner will "consume" the part of the buffer that is an integer.  When you enter this from the keyboard, the <enter> leaves (depending on the OS) a CR, LF, or CRLF at the end.

* If you call scanner.nextInt() again, it knows to move past the <enter> and consume the integer.  There is still an <enter> at the end of the buffer.

* Now you call scanner.nextLine().  All nextLine() knows is to consume the buffer up to the end-of-line, which looks just like the <enter> character(s).  But you already have an <enter> in the buffer, so it consumers this and returns an empty String without you entering anything.

The easiest way around the problem is to call nextLine() twice, once to consume the <enter> at the end and once to get your actual input.


Thanks Knute =)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!