property website
property website

Reputation: 41

Running a timertask is not working because of unusual syntax error

Newbie question:I have 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 m getting is on timer.schedule(task, 2000, 5000);

The error is called syntax error ")" delete this token

I have checked and the code would not run. without it aswell

Upvotes: 3

Views: 69

Answers (1)

Anonymous
Anonymous

Reputation: 86359

This is a statement:

        timer.schedule(task, 2000, 5000); 

A statement needs to go inside a method. Add a main method to your class and put your declarations and your statement there.

Upvotes: 3

Related Questions