This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes working with Scanner Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "working with Scanner" Watch "working with Scanner" New topic
Author

working with Scanner

Theresa Marlin
Ranch Hand

Joined: Sep 23, 2009
Posts: 49
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
Sheriff

Joined: Sep 28, 2004
Posts: 18139
    
  39

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Theresa Marlin
Ranch Hand

Joined: Sep 23, 2009
Posts: 49
This makes sense, thanks!
 
Consider Paul's rocket mass heater.
 
subject: working with Scanner
 
Similar Threads
Using Lists/ArrayLists, code doesn't work
Convert Queue Dequeue to Link list format
Need help ending number guessing game and asking if user wants to play again
Please help, java program terminating unexpectedly without reason
how to access instance variables