Java – Running timertask does not work due to unusual syntax errors

Running timertask does not work due to unusual syntax errors… here is a solution to the problem.

Running timertask does not work due to unusual syntax errors

Newbie question: I’ve been trying to run a timer. But I get an unusual error. Here is the code below.

class Helper extends TimerTask 
{ 

public static int i = 0; 

public void run() 
    { 
        System.out.println("Timer ran " + ++i); 
    } 
} 

public class Test 
{ 
    Timer timer = new Timer(); 
    TimerTask task = new Helper(); 
    timer.schedule(task, 2000, 5000); 
} 

The error I get is
timer.schedule(task, 2000, 5000);

The error is called a syntax error”)”delete this token

I’ve checked, the code doesn’t work. It’s the same without it

Solution

Here is a statement:

        timer.schedule(task, 2000, 5000); 

The statement needs to go inside the method. Add the main method to your class and put your declarations and statements there.

Related Problems and Solutions