I'm trying to write a timer that will count down from 10 and then stop when it gets to 0 but in the 'timer.cancel();' part at the bottom of the code, there is a red line underneath 'timer'. It says that I haven't defined 'timer'. I did define 'timer' earlier on in the code so I'm not sure what I'm doing wrong?
(I am doing this in NetBeans 8.2)
package javaapplication3;
import java.util.Timer;
import java.util.TimerTask;
public class JavaApplication3 {
public static void main(String[] args) {
Timer timer = new Timer();
timer.schedule(new timeInterval(), 0, 1000);
}
}
class timeInterval extends TimerTask {
int countdown = 10;
public void run() {
countdown = countdown - 1;
System.out.println(countdown);
if (countdown <= 0) {
timer.cancel();
}
}
}