EvilZone

Programming and Scripting => Java => Topic started by: Super_mario666 on March 13, 2013, 05:04:39 AM

Title: Pause a program?
Post by: Super_mario666 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.
Title: Re: Pause a program?
Post by: parad0x 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.