I'm brand new to this and was building a simple clock/timer program. It was very simple with methods to set the time, get the time, and start the clock. Everything works, even the timer. The only problem is when the timer first starts, it produces an error message:
"Exception in thread "main" java.lang.IllegalStateException: Task already >scheduled or cancelled at java.util.Timer.sched(Timer.java:401) at java.util.Timer.scheduleAtFixedRate(Timer.java:328) at Clock.start(Clock.java:53) at ClockTest.main(ClockTest.java:46) The time is: 5:55:45 The time is: 5:55:46 The time is: 5:55:47 The time is: 5:55:48 The time is: 5:55:49 The time is: 5:55:50 The time is: 5:55:51"
From what I understand, this means that there is another timer already started, but I don't see where that is in my code. To clarify, this error is produced when I start the timer, but then the timer works.
Here is my code. Here's the main method:
import java.util.Scanner;
public class ClockTest{
public static void main (String [] args){
int userInput, hr, min, sec;
Clock clock = new Clock();
Scanner scan = new Scanner(System.in);
System.out.println("What would you like to do?\n1) Set Clock"
+ "\n2) Get the time the clock is set to \n3) Start the timer\n"
+ "4) Exit");
userInput=scan.nextInt();
while (userInput!=4){
if (userInput==1){
System.out.println("Enter the hour: ");
hr=scan.nextInt();
System.out.println("Enter the minutes: ");
min=scan.nextInt();
System.out.println("Enter the seconds: ");
sec=scan.nextInt();
clock.setTime(hr,min,sec);
System.out.println("What would you like to do?\n1) Set Clock"
+ "\n2) Get the time the clock is set to \n3) Start the timer\n"
+ "4) Exit");
userInput=scan.nextInt();
}if(userInput==2){
System.out.println(clock.getTime());
System.out.println("What would you like to do?\n1) Set Clock"
+ "\n2) Get the time the clock is set to \n3) Start the timer\n"
+ "4) Exit");
userInput=scan.nextInt();
}if(userInput==3)
clock.start();
}
}
}
And here is the Clock class that I created:
import java.util.Timer;
import java.util.TimerTask;
public class Clock {
int seconds=0;
int hours=0;
int minutes=0;
TimerTask hourTask = new TimerTask(){
public void run() {
seconds++;
if(seconds==60 && minutes!=60 && hours!=60){
seconds=0;
minutes++;
}if(seconds==60 && minutes ==60 && hours!=60){
seconds=0;
minutes=0;
hours++;
}if(seconds==60 && minutes==60 && hours==60){
seconds=0;
minutes=0;
hours=0;
}System.out.println("The time is: " + hours + ":" + minutes + ":" + seconds);
}
};
//---------------------------------------------------------------------
//constructor without parameters
//---------------------------------------------------------------------
public Clock (){
hours=0;
minutes=0;
seconds=0;
}
//---------------------------------------------------------------------
//constructor with parameters
//---------------------------------------------------------------------
public Clock (int hrs, int min, int sec){
hours=hrs;
minutes=min;
seconds=sec;
}
//---------------------------------------------------------------------
//starts the clock
//---------------------------------------------------------------------
public void start(){
Timer hourTimer = new Timer();
hourTimer.scheduleAtFixedRate(hourTask, 1000, 1000);
}
//---------------------------------------------------------------------
//sets the time
//---------------------------------------------------------------------
public void setTime(int hrs, int min, int sec){
hours=hrs;
minutes=min;
seconds=sec;
}
//---------------------------------------------------------------------
//gets the time
//---------------------------------------------------------------------
public String getTime(){
return "The time is: " + hours + ":" + minutes + ":" + seconds;
}
}
I'm new to this all so if anyone could answer this, I'd greatly appreciate it. Also, if you see any way I can improve or clean up my code, I welcome all critiques.
Also - fyi - I write everything in TextEdit still (my prof. is big on tough learning), so if it may not look pretty.
Thank you