import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class program
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
List <String> names =new ArrayList<String>();
System.out.println("Enter the count: ");
int name1 = scanner.nextInt();
for (int i =0; i < name1; i++)
{
int count = i+1;
System.out.println("Enter the "+ count +" name: ");
names.add(scanner.nextLine());
}
System.out.println("\n");
for (String k : names)
{
System.out.println(k);
}
scanner.close();
}
}
the objective is to get the count of names from users and then store the names via user input of the names through a for-loop in a list, and then print the names stored. But when I execute the code, the first iteration of the last for-loop skips user input and jumps to the second iteration.
The output is shown below
Enter the count:
4
Enter the 1 name:
Enter the 2 name:
ashley
Enter the 3 name:
timmy
Enter the 4 name:
paul
ashley
timmy
paul
As you can see the user is not able to input the 1st name since it jumps to the 2nd name directly. would greatly appreciate anyone's help, and sorry if my question lacks context as this is my first time posting a question and I'm new to java.