0
Scanner in = new Scanner(System.in);
for (int i = 1; i <= 6; i++) {
    System.out.print("Name: ");
    in.nextLine();
    System.out.print("Age: ");
    in.nextInt()
}

I expect the output of:

Name: David Obama

Age: 14

Name: Ava Omidi

Age: 53

etc.

but this is my output and only once nextLine() is used:

Name: David Obama

Age: 14

Name: Age:

halfer
  • 18,701
  • 13
  • 79
  • 158
abcd ef
  • 49
  • 7

1 Answers1

1

If you called nextInt or nextDouble you need to call scan.nextLine(); command immediately after that, to read any string properly.

Scanner in = new Scanner(System.in);
for (int i = 1; i <= 6; i++) {
    System.out.print("Name: ");
    in.nextLine();
    System.out.print("Age: ");
    in.nextInt()

    //you need to make another call to nextLine() because of the Scanner object
    //will read the rest of the line where its previous read left off. 
    in.nextLine();
}
Ali Azim
  • 142
  • 14