Count and Print 1 to 10 in Real Time - Help :S
Hi all,
I need to make a Program that once the user clicks run, the Program prints out 1 to 10 with a second gap in between so it actually prints out in real time:
The output would just be:
1
2
3
4
5
6
7
8
9
10
But would print 1, then wait a second then print 2, then wait a second then print 3, etc. etc. until 10. Then the Program would just finish as normal.
How easy is would this be ? Im not asking for code but if someone could point me in the right direction as to what classes or methods i need to be using, and any advice you feel necessary, then that would be fantastic. Bearing in mind i am a beginner.
P.S. If i havn't made the requirements entirely clear then please don't hesitate to ask a question.
Re: Count and Print 1 to 10 in Real Time - Help :S
That should be easy. Look at the Thread class's sleep() method to handle the delay inside of a loop.
Re: Count and Print 1 to 10 in Real Time - Help :S
Sounds like a simple call to Thread.sleep() would do the trick.
Alternatively you could use a Timer.
Re: Count and Print 1 to 10 in Real Time - Help :S
It doesnt seem to like this:
Code :
public static void main(String[] args) {
for (int counter = 1; counter <= 10; counter++){
System.out.println(counter);
Thread.sleep(1000);
}
}
}
Thread.sleep(1000); is red underlined and says:
Quote:
Exception in thread "main" java.lang.RuntimeException:
Do i need to throw an exception ?
Re: Count and Print 1 to 10 in Real Time - Help :S
Can you post the full text of the error message that shows where the exception happened?
You don't need to throw an exception.
The code you posted should generate a compile time error, not a runtime error. Just compile it, don't try to execute it.
Re: Count and Print 1 to 10 in Real Time - Help :S
Here is the full error:
Quote:
run:
1
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - unreported exception java.lang.InterruptedException; must be caught or declared to be thrown
at latesttasks.task15.main(task15.java:14)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
regards.
Re: Count and Print 1 to 10 in Real Time - Help :S
Quote:
unreported exception java.lang.InterruptedException; must be caught or declared to be thrown
This part says that the sleep() method can throw an exception. Your code must be ready to handle that exception. The normal way is to put that statement inside of a try{}catch(){} block to "catch" the exception.
Re: Count and Print 1 to 10 in Real Time - Help :S
Having added the try catch block i have the following:
Code :
package latesttasks;
// Count and Print 1 to 10 in Real Time.
// Threads
import java.util.*;
public class task15 {
public static void main(String[] args) {
try {
for (int counter = 1; counter <= 10; counter++) {
System.out.println(counter);
Thread.sleep(1000);
}
catch (InterruptedException e){
}
}
}
But the catch line is red underlined and the program now runs and prints the 10 numbers but then the error occurs.
here is the output:
Quote:
run:
1
2
3
4
5
6
7
8
9
10
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - 'catch' without 'try'
at latesttasks.task15.main(task15.java:15)
Java Result: 1
BUILD SUCCESSFUL (total time: 11 seconds)
Re: Count and Print 1 to 10 in Real Time - Help :S
Check that the {}s are properly paired.
Re: Count and Print 1 to 10 in Real Time - Help :S
They appear to be correctly paired
I should also add 'sleep' is yellow underlined and it says 'Thread.sleep called in loop'
--- Update ---
Update:
All fixed. Thanks for your help.