Java Using Scanner in Java to await user hitting return key

  • Thread starter Thread starter mr.hood
  • Start date Start date
  • Tags Tags
    Java Scanner
AI Thread Summary
To pause a Java program until the user hits the return key, using the Scanner class alone is insufficient, as it requires actual input rather than just a key press. Instead, a KeyListener from the java.awt.event library is recommended. This approach allows the program to listen for the Enter key event, enabling it to proceed without waiting for additional input. Implementing a listener can effectively manage user interactions in a more flexible manner than the Scanner class.
mr.hood
Messages
6
Reaction score
0
Hi all,

I've been trying to figure out how I can use the Scanner class in Java to pause my program and wait for the user to hit the return key. What I have now looks like this:

Scanner kbd = new Scanner(System.in);
kbd.next();
...

The thing is, the rest of my program won't run unless there is actually some kind of input (i.e. just hitting the return key without any input just makes the program keep waiting for input). Is there a way I can get around this?
 
Last edited:
Technology news on Phys.org
I don't think that scanner has that functionality. What scanner.next() does is it was for user input and returns the input. It does not accept null characters.

What your looking for is some type of listener. Take a look at the KeyListener class from java.awt.event.* libary. Just perform some wait function and listen for the enter key.
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
Back
Top