Сканер пропускает nextLine () после использования next () или nextFoo ()?

Я используюScanner методыnextInt() а такжеnextLine() для чтения ввода.

Это выглядит так:

System.out.println("Enter numerical value");    
int option;
option = input.nextInt(); // Read numerical value from input
System.out.println("Enter 1st string"); 
String string1 = input.nextLine(); // Read 1st string (this is skipped)
System.out.println("Enter 2nd string");
String string2 = input.nextLine(); // Read 2nd string (this appears right after reading numerical value)

Проблема в том, что после ввода числового значения первоеinput.nextLine() пропускается и второйinput.nextLine() выполняется, так что мой вывод выглядит так:

Enter numerical value
3   // This is my input
Enter 1st string    // The program is supposed to stop here and wait for my input, but is skipped
Enter 2nd string    // ...and this line is executed and waits for my input

Я протестировал свое приложение, и похоже, что проблема заключается в использованииinput.nextInt(), Если я удалю его, то обаstring1 = input.nextLine() а такжеstring2 = input.nextLine() исполняются так, как я хочу, чтобы они были.

Ответы на вопрос(15)

Ваш ответ на вопрос