I am trying to do something in Java and I need something to wait / delay for an amount of seconds in a while loop.
while (true) {
if (i == 3) {
i
I know this is a very old post but this may help someone:
You can create a method, so whenever you need to pause you can type pause(1000)
or any other millisecond value:
public static void pause(int ms) {
try {
Thread.sleep(ms);
} catch (InterruptedException e) {
System.err.format("IOException: %s%n", e);
}
}
This is inserted just above the public static void main(String[] args)
, inside the class. Then, to call on the method, type pause(ms)
but replace ms
with the number of milliseconds to pause. That way, you don't have to insert the entire try-catch statement whenever you want to pause.