I have a timer class that prints out a string every 5 ticks, and on the 6th tick it runs a random event. now what i want is to have the timer to restart from 9 ticks. As i tried to do with the else statement in the MyTask Class withing the TimerSample class below.

And one more question: How can i implement an if statement in the the main class so that i can have the user to enter a value inorder to run the timer?

Thanks in advance!!

The Class

import java.util.Timer;
import java.util.TimerTask;
import java.util.Random;
import java.util.Scanner;
public class TimerSample {
 
    public static void main(String[] args) {
      
        Timer timer = new Timer("Printer");
        MyTask t = new MyTask();
        timer.schedule(t, 0, 2000);
 
 
    }
}
 
class MyTask extends TimerTask {
  
    private int times = 0;
 
 
    public void run() {
        times++;
        
        if (times <= 5) {
            System.out.println("Time is Passing...");
        }else if(times > 5) {
            
            Random rand = new Random();
            RandomEvents newREvent = new RandomEvents(rand.nextInt(30));
   
        }else if(times == 7){
            times = 0;
        }
            
        
       
    }
}

Recommended Answers

All 6 Replies

Why is your forst question a problem? Your code counts 5, does something random, counts on up to 7 then starts again. How is that different from what you want?


Which value do you want the user to enter?

Why is your forst question a problem? Your code counts 5, does something random, counts on up to 7 then starts again. How is that different from what you want?

Well it is not restarting the timer after the 7 ticks. and i wanted to know why.

Which value do you want the user to enter?

and I want the user to enter anything using the scanner class and implement an if statement to run the timer and after the 7 ticks it should prompt the user to run it again or not.

You don't stop the timer, so why are you talking about restarting it?

You don't stop the timer, so why are you talking about restarting it?

ok nevermind that i found that bluej compiler is really buggy i so i copy and pasted everything in eclipse and it works fine..

so aside from that point how can i make it so the timer starts upon command only.

for example

System.out.println("Press yes for a Random event");
        String choice = in.next();
        if(choice.equalsIgnoreCase("yes"))
        { timer.schedule(t, 0, 2000);}

and after the 6 ticks are done it should go back to prompting the user to enter a command to start the timer again.

Yes, I only use Eclipse... it has so many "industrial" users that it has to be solid.

After 6 ticks you can cancel() the current Timer and prompt the user again.
When the user types yes (as in your code above) call its schedule() again

Yes, I only use Eclipse... it has so many "industrial" users that it has to be solid.

After 6 ticks you can cancel() the current Timer and prompt the user again.
When the user types yes (as in your code above) call its schedule() again

ok thanks got it to work thought that wouldnt work but i guess it did nevermind lol
thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.