Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

JAVA Scanner Issue!

 
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:

 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE 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
    Bookmark Topic Watch Topic
  • New Topic