I'm assuming you mean in a console application.... You can use a Scanner. The hasNextLine method will return true if they enter anything. You can follow that with a call to nextLine which will remove that line from the input stream and return it to you. If you literally only want to proceed if they pressed only the enter key and nothing else then you can check if the string returned is the empty string.
Scannerin=newScanner(System.in);System.out.println("Press enter to continue.");if(in.hasNextLine()){Strings=in.nextLine();// s will be whatever they entered without new line character.// Even if you don't need to know what they entered, // call nextLine or else it will continue to sit on input stream// and lead to subsequent calls to hasNextLine// to evaluate to true before they enter anything else.}// You will only get here after they enter something.System.out.println("Yay! You pressed enter.");
Note that the hasNextLine will never return false if Scanner reading from System.in. It will wait for input and then return true.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I'm assuming you mean in a console application.... You can use a
Scanner. ThehasNextLinemethod will return true if they enter anything. You can follow that with a call tonextLinewhich will remove that line from the input stream and return it to you. If you literally only want to proceed if they pressed only the enter key and nothing else then you can check if the string returned is the empty string.Note that the
hasNextLinewill never return false if Scanner reading fromSystem.in. It will wait for input and then return true.