• Post Reply Bookmark Topic Watch Topic
  • New Topic

JAVA Scanner Issue!  RSS feed

 
Bryan Smith
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My code works fine until the incorrect word is guessed, then the scanner(which works on the first run), seems to just act as if the user has hit enter of an empty space, thus causing an error. Please help.

Words for text file:

MIGHTY
crimes
FLIGHT
FRIGHT
Grimes
PLACES
TRACES
plates
Fisher
fishes
WISHES
dishes

The error:



My code:

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bryan Smith wrote:My code works fine until the incorrect word is guessed, then the scanner(which works on the first run), seems to just act as if the user has hit enter of an empty space, thus causing an error.

Almost invariably, this is because you're not consistent about the way you enter data. In your case, you use nextLine() to get everything except the 'choice' value at line 275, which means that the Scanner still has a newline in its buffer, which is consumed by the next nextLine() call (probably when trying to get the first guess for a new game).

Just use nextLine() everywhere, and I suspect it'll fix the problem.

Also: your 'while' check at line 84 is wrong. If you're calling nextLine(), you need to use hasNextLine().

You might find the UserInput page worth reading on the subject, but I warn you: it's not short.

HIH

Winston
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!