Using Scanner in Java to await user hitting return key

  • Java
  • Thread starter mr.hood
  • Start date
  • #1
8
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:

Answers and Replies

  • #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.
 

Related Threads on Using Scanner in Java to await user hitting return key

  • Last Post
Replies
2
Views
10K
Replies
4
Views
617
Replies
4
Views
270
Replies
4
Views
4K
  • Last Post
Replies
6
Views
2K
Replies
1
Views
1K
  • Last Post
Replies
3
Views
1K
  • Last Post
Replies
2
Views
3K
  • Last Post
Replies
4
Views
22K
Top