0

This is a snippet of my code:

public static void main(String[] args) {
    System.out.println("This is the square root game. Only two players allowed.");

    Scanner scan = new Scanner(System.in);
    System.out.println("How many rounds are you two playing?");
    int numofRounds = scan.nextInt();

    System.out.println("First player, what is your name?");
    String firstPlayer = scan.nextLine();
    System.out.println();

    System.out.println("Second player, what is your name?");
    String secondPlayer = scan.nextLine();
    System.out.println();

Everything is ok: it reads in the next integer for numofRounds, and it reads in the next Line for secondPlayer, but it skips firstPlayer entirely and does not allow me to input for it. Why is this? Thanks in advance :)

Got it thanks!

Freedom
  • 347
  • 2
  • 7
  • 24
  • after `int numofRounds = scan.nextInt()` add in the line `scan.next()`. I can't remember exactly what it is, but there's an issue with java scanning integers and whitespace – MacSalty Nov 05 '13 at 00:23
  • @Anonymity Basically, if the integer was the last thing on its line, the trailing newline isn't read in by `nextInt()`, so it will be read in by the subsequent `nextLine()`. – Dennis Meng Nov 05 '13 at 00:24
  • @DennisMeng Thank you! I was trying to remember from my Java class what that reason was, but I believe that's what is causing this issue. – MacSalty Nov 05 '13 at 00:25
  • @Anonymity It is. I've seen it enough times to recognize when it happens; it pops up in some form or another here every couple days or so. – Dennis Meng Nov 05 '13 at 00:27
  • Ohhhhh thanks a lot Anonymity and especially Dennis Meng for helping me – Freedom Nov 05 '13 at 00:29

0 Answers0