This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

working with Scanner

 
Theresa Marlin
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to wrote the following code:




and every time I compiled and ran it, it would skip over the input of player 1's name, and go right to player 2. As far as I can see, the two are identical in syntax. Any suggestions?
 
Henry Wong
author
Marshal
Pie
Posts: 21212
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Theresa Marlin wrote:
and every time I compiled and ran it, it would skip over the input of player 1's name, and go right to player 2. As far as I can see, the two are identical in syntax. Any suggestions?


Basically, when you asked for "how many rounds?", you only read an int. This doesn't read a whole line, it just reads an int token. Later, when you ask for the first player, you read a line. This reads from the current location to the end of the line. The current location is just after the int -- so the first player is the blank string from the end of the int to the carriage return of the line.

Henry
 
Theresa Marlin
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This makes sense, thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic