This issue arises because Scanner.next() only reads until the next white space. When you press the enter after entering an invalid input, the newline character remains buffer. On the next iteration, next() might pick it up
And you use the scanner.nextLine() to clear the buffer introduces a new line in the output