I’m working on an example in Daniel Liang’s Introduction to Java, and I’m trying to step through the problem to see what exactly is happening, but everything prints out too quickly. So, I’m looking for an equivalent in Java to Python’s time.sleep().
I know I could make a timer via currentTimeMillis, but a simple sleep function would be much more handy.
I’ve tried typing Thread.sleep(millis) as suggested on google — as well as Thread.getCurrentThread.sleep(), but both are giving the following error:
error: unreported exception InterruptedException; must be caught or
declared to be thrown
Thread.sleep(1000);
This is the piece of code in question:
for (int j = i; j<100; j+=i){
Thread.sleep(1000);
System.out.println(j);
}
put the Thread.sleep inside try catch block like