It's because when you enter a number then press Enter, input.nextInt()
consumes only the number, not the "end of line". Primitive data types like int, double etc do not consume "end of line", therefore the "end of line" remains in buffer and When input.next()
executes, it consumes the "end of line" from buffer from the first input. That's why, your String sentence = scanner.next()
only consumes the "end of line" and does not wait to read from keyboard.
Tip: use scanner.nextLine()
instead of scanner.next()
because scanner.next()
does not read white spaces from the keyboard. (Truncate the string after giving some space from keyboard, only show string before space.)