Author Topic: Pause a program?  (Read 2708 times)

0 Members and 1 Guest are viewing this topic.

Offline Super_mario666

  • Knight
  • **
  • Posts: 160
  • Cookies: 7
  • Professional Badass
    • View Profile
Pause a program?
« on: March 13, 2013, 05:04:39 am »
is there a way in java to pause a program until the user enters a key.

Code: [Select]

try {
Thread.sleep(2000);
}
catch(InterruptedException e) {
}


i already know this ^
i wanted to know if there is an equivalent to 'getch()' in java.
« Last Edit: March 13, 2013, 05:05:28 am by Super_mario666 »
The Bigger they are...The more likely you'll get your ass kicked

Offline parad0x

  • VIP
  • Royal Highness
  • *
  • Posts: 638
  • Cookies: 118
    • View Profile
Re: Pause a program?
« Reply #1 on: March 13, 2013, 05:18:26 am »
You can make an object of class BufferedReader and then you can use:

Code: [Select]
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter a key to continue: ");
br.readLine();

In this way, it'll act as 'getch()' function in C.

You can do this with Scanner also. What you do in getch() and here is you asks user for input but doesn't assign that value to any variable.

Code: [Select]
Scanner input = new Scanner(System.in);
/* Other code
*/
input.nextLine();

We have done the same thing in both the examples and what you have presented is sleeping the program 2000 miliseconds, you can adjust that as you want.
« Last Edit: March 13, 2013, 05:38:53 am by parad0x »