sc.nextline skips 1

sc.nextline skips

int option = input.nextInt();
input.nextLine();  // Consume newline left-over
String str1 = input.nextLine();

Here is what the above code is Doing:
1. It’s asking the user to enter an integer.
2. It’s asking the user to enter a string.
3. It’s asking the user to enter an integer.
4. It’s asking the user to enter a string.

The problem is that the second input.nextLine() call is reading the newline character left-over from the first input.nextLine() call.

To fix this, we can add an extra input.nextLine() call after the first input.nextLine() call to consume the newline character.

Similar Posts