Reputation: 479
Is there any functionality in Java for pausing the program like system("PAUSE");
does in C++?
Upvotes: 5
Views: 63711
Reputation: 19
There is a cin.get(), or cin.ignore() thing in C++, which basically makes you press the enter key to to exit the program, I know it is cheesy, but it works. What you could do is setup a scanner with a .nextLine(), like this
import java.util.Scanner;
public class Example {
public static void main(String args[]){
Scanner pauser = new Scanner (System.in);
System.out.println("!!!Hello World!!!");
pauser.nextLine();
}
}
then the user must press enter to exit the program.
Upvotes: 1
Reputation: 180
The Short Is Yes, But You Have To Build It Using The Standard Output With System.out
and Using The Scanner
Class To Wait For New Line Input ('\n') From Standard Input From The System.in
public class PauseTest {
public static void main(String args[]){
System.out.println("Press Any Key To Continue...");
new java.util.Scanner(System.in).nextLine();
}
}
NOTE: I used a direct reference to the Scanner
Class in java.util
package, but you could simply add the import java.util.Scanner;
or import java.util.*;
Basically the same thing.
Upvotes: 13
Reputation: 13114
If you are attempting to debug, use a debugger. Netbeans and Eclipse both have one built in, and I am sure there are others.
If you are trying to get a thread to wait on another thread, use a thread.join().
Upvotes: 0
Reputation: 1046
Doing debugging is best done with a debugger, NetBeans (what you seem to be using if I look at other questions you have asked) has one. Just click in the left margin of an editor window to set a breakpoint and run the debugger. Program execution will stop at the breakpoint and you can have a look at the current state of variables, or step through the program line by line.
Upvotes: 8
Reputation: 129774
I don't know Java very well, but if you want to debug, then you may want to use debugger, and breakpoints for pausing execution.
If you just want to wait for keyboard input, then check out System.in.read
, or something similar.
Upvotes: 1
Reputation: 29157
You mean like Thread.sleep()? Please elaborate if you want further help...
Upvotes: 2