Search code examples
javaeclipsevalidationjava.util.scannersystem.in

How does java treat the input of the "enter" key on a keyboard


hi complete noob in programming and am trying to do some user input validation using a System.in scanner i am trying to find a way to detect when a user inputs nothing and just presses the enter key but it seems like java doesn't acknowledge the enter key as an input because when i run the program and press enter the text cursor on the console just moves to a new line and the program acts like it is still waiting for an input so how would i detect when the enter key is pressed?

side question is pressing the enter key the same as no input or is it classes as input?


Solution

  • If you're using an instance of Scanner(System.in), pressing the enter key, scanner.nextLine() without a previous input should return an empty string.