Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Using Scanner in Java to await user hitting return key

  1. Oct 28, 2008 #1
    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);

    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: Oct 28, 2008
  2. jcsd
  3. Oct 28, 2008 #2
    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.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: Using Scanner in Java to await user hitting return key
  1. Java Scanner (Replies: 2)