We help IT Professionals succeed at work.

press any key to continue in java

Timo13
Timo13 asked
on
How do I make the program wait until a key is pressed to continue?

class Main
{
  public static void main(String[] arg)
  {
    System.out.println("press any key to continue");
    // wait here
    System.out.println("abc");
  }
}
Comment
Watch Question

Chad SmithSoftware Engineer
BRONZE EXPERT

Commented:
This solution works with the enter key

class Main
{
 public static void main(String[] arg)
 {
   System.out.println("press enter to continue");
   try {
     System.in.read();
   }
   catch (java.io.IOException ioe) {
     System.out.println("IOException: " +ioe);
   }
   System.out.println("abc");
 }
}

Commented:
Seem like you want the similar function to getch() in C. The getch() in C is a DOS-specific extension to the C stdio library. The only way is writing native code to implement that.

Author

Commented:

Author

Commented:
That press enter "press enter to continue" is what I am using now. I want to use press any key, not only enter. Isn't there a way to see wen a key is pressed on the keyboard?
Hey Timo

//off-topic: How ya doing fellow earthling?

Well i think it can't be programmed in Java..
The idea of the Enter-key is good, but i don't think it can be done with "Any Key" !

Well i think you just have to use a thread.wait()...

Hope this can help you!

Atuzzz

Author

Commented:
Hi Blizzard :D
Nice to see you made it to experts-exchange. ;)

Here are the points but that's a terrible answer.