That’s because the Scanner.nextInt method does not read the newline character in your input created by hitting “Enter,” and so the call to Scanner.nextLine returns after reading that newline.
You will encounter the similar behaviour when you use Scanner.nextLine after Scanner.next() or any Scanner.nextFoo method (except nextLine itself).
Workaround:
- Either put a
Scanner.nextLinecall after eachScanner.nextIntorScanner.nextFooto consume rest of that line including newlineint option = input.nextInt(); input.nextLine(); // Consume newline left-over String str1 = input.nextLine(); - Or, even better, read the input through
Scanner.nextLineand convert your input to the proper format you need. For example, you may convert to an integer usingInteger.parseInt(String)method.int option = 0; try { option = Integer.parseInt(input.nextLine()); } catch (NumberFormatException e) { e.printStackTrace(); } String str1 = input.nextLine();