• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why are these Scanners being skipped?  RSS feed

 
John Deacon
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello! New to this site, but often go here to read questions.
I've been encountering this weird error (Eclipse doesn't throw an exception) in which one of the scanners are being skipped. I've searched on google, but nothing shows.

Here's the code: (My apologies for the formatting)




There should be nothing wrong with this code. For what I've tried, I declared new scanner in every method,which fixed the problem. But I've been using a static Scanner for the whole class many times with no problems. What's causing this? Thanks
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Deacon wrote:I've been encountering this weird error (Eclipse doesn't throw an exception) in which one of the scanners are being skipped.

What do you mean by "skipped"?

I suspect that the problem is that you type in "1" and press the ENTER key, which is NOT consumed by the Scanner when it executes nextInt(), which means that the first nextLine() call returns a null String.

Just one of the reasons that I dislike Scanner (... pause ... wait for Campbell's response ... ).

BTW: I wouldn't recommend this style - neither static Scanners, nor static methods all over the place - it may work for a while, but it's really not very scalable. For an alternative, you might want to read the MainIsAPain page

Winston
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It wouldn't be so bad if it were documented anywhere. Most of the books I have read misunderstand nextLine totally.
 
John Deacon
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
John Deacon wrote:I've been encountering this weird error (Eclipse doesn't throw an exception) in which one of the scanners are being skipped.

What do you mean by "skipped"?

I suspect that the problem is that you type in "1" and press the ENTER key, which is NOT consumed by the Scanner when it executes nextInt(), which means that the first nextLine() call returns a null String.

Just one of the reasons that I dislike Scanner (... pause ... wait for Campbell's response ... ).

BTW: I wouldn't recommend this style - neither static Scanners, nor static methods all over the place - it may work for a while, but it's really not very scalable. For an alternative, you might want to read the MainIsAPain page

Winston


I'm sorry if I'm not getting it, but aren't you supposed to press enter anytime scanner is being called so the input is accepted? Also, "x" is an integer and I'm calling Scanner for the nextInt().

Oh, and my mistake, but id() was also meant to run. I commented it out by mistake.
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but there is a problem with next line. What does your book say nextLine() does?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Deacon wrote:I'm sorry if I'm not getting it, but aren't you supposed to press enter anytime scanner is being called so the input is accepted?

Not necessarily. You can enter several things on the same line by separating them with spaces; it's just that we're used to pressing Enter key when we input values, and when you do, the problem is that Scanner.nextInt() doesn't consume it.

I suspect that if you changed line 12 from:

int x = kb.nextInt();

to:

int x = Integer.valueOf( kb.nextLine() );

everything will work just fine, providing you enter a number. If you don't ... well, let's just say that that's where all the problems start with user input.

If you're interested, the UserInput page provides one way of doing it. It's quite long, and divided into 3 pages, the third of which isn't complete yet; but you may get some ideas just from reading the first one.

But remember: it's just ONE way of doing it - there are many others - and it's definitely not one that Campbell would agree with.

HIH

Winston
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!