1

I'm fairly new to Java and programming in general. I'm trying to ask for names of celebrities and put them in an array. When I try to use scanner to read input from the user, it manages to store the name, but runs the line System.out.println("Name is too short, try again"); before the condition of the loop.

Here are the relevant parts from my code:

public static final Scanner scan = new Scanner(System.in);

public static void main(String[] args) {

    String celeb[];
    int number;
    System.out.println("Welcome to Celebrity Guesser!");
    System.out.println("How many celebrities do you want to name?");
    number = scan.nextInt();
    celeb = new String[number];

    for(int i = 0; i < number; i++) {   //asks for user input
        celeb[i] = celebInput(i + 1);
    }
    for(int i = 0; i < number; i++) {   //asks questions to user
        String tempCeleb = celeb[i];
        celebOutput(tempCeleb, i + 1);
    }
}

//Reads names of celebrities from the user
static String celebInput(int n) {
    System.out.println("\nType in celebrity #" + n);
    String c = scan.nextLine();
    while(c.length() < 6) {
        System.out.println("Name is too short, try again");
        c = scan.nextLine();
    }
    return c;
}

Here is the output:

Welcome to Celebrity Guesser!
How many celebrities do you want to name?
2

Type in celebrity #1
Name is too short, try again
Miley Cyrus

Type in celebrity #2
Katy Perry

Why does the issue occur only for "celebrity #1" and not "celebrity #2"? Thank you.

ipwnmice
  • 15
  • 5
  • The code is incomplete - we are mising the `celebOutput` method – Andersnk Feb 26 '15 at 00:22
  • the output works fine, all the information is saved to the array. The issue is that it says the name is too short before the user has a chance to enter a name. Thanks! – ipwnmice Feb 26 '15 at 00:24
  • @AndersNK The `celebOutput()` method probably isn't relevant to the code's operation. – APerson Feb 26 '15 at 00:24
  • @ipwnmice It looks like there's a newline hanging around in the buffer, and the Scanner is simply picking that up. Silly question: are you sure you're running the code without hitting enter too many times? – APerson Feb 26 '15 at 00:25

2 Answers2

0

scan.nextInt will remove only the "2" from the input stream. The "\n" (new line) you enter when you press enter is still in the input stream; the first nextLine call will therefore return an empty line.

To clarify:

    // assuming we enter "2\ntest\n" at the command line
    Scanner s = new Scanner(System.in); // s now contains "2\ntest\n"
    System.out.println(s.nextInt()); // s now contains "\ntest"
    System.out.println(s.nextLine()); // s now contains "test"
    System.out.println(s.nextLine()); // s is empty

Output:

"2"
""
"test"

The solution would be to call nextLine after nextInt to clear the empty line.

Adrian Leonhard
  • 6,220
  • 2
  • 21
  • 37
0

Try number = Integer.parseInt(scan.nextLine()); instead of number = scan.nextInt();

I hope it works. Let me know.

Prudhvi
  • 2,108
  • 7
  • 31
  • 51