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.