For part of my project, I need to write a piece of code that will perform a task (beeping) once per second for ten seconds. I found this code online and tried to modify it to fit my needs, but I couldn't seem to get it. This code doesn't beep, it just prints "doing" every second, but that's not important - I can change that later. I just need help in figuring out how to make it print "doing" only ten times, and not keep going until the program is manually stopped. Any help is appreciated. :)

import java.util.Timer;
import java.util.TimerTask;

public class AnnoyingBeep {
  public static void main(String[] argv) throws Exception {
    int delay = 5000; // delay for 5 sec.
    int period = 1000; // repeat every sec.
    Timer timer = new Timer();


    timer.scheduleAtFixedRate(new TimerTask() { 
      public void run() {
        System.out.println("doing");
    }

    }, delay, period); 
  }
}

Note/Disclaimer: The code isn't mine - I copied it off a website somewhere. I don't know which website it was or who wrote it, but I have no intention of using this code to make money or redistribute, or of turning it in for school credit. //import legalese;

All I would do is create a separate thread and start it whenever I want:

class MyThread extends Thread {

}
MyThread mt = new MyThread();
mt.start();

Now in the run method you can try something like this:

int count = 0;
while (count<10) {
  try {
     Thread.sleeep(1000);
     // beep;
  } catch (Exception e) {
  }
  count++;
}

Since the while repeats every 1 second, you can use the "count" variable to count 10 seconds.

Or you can use the "System.currentTimeMillis()" method to get the time in milliseconds and save it into a variable.
At the while statement compare that variable with the currentTimeMillis taken each time and if their difference is more than 10 seconds (10000 millis) then stop the loop

I have to disagree with javaAddict (yes, dangerous, I know). The Timer class is there so you don't have to build your own bomb-proof threading code. Moreover, if this is part of a Swing application, you should use the swing Timer, not the util one, because that integrates properly with the swing event handling.
Either way, just create an int counter, increment it every time you beep, and stop the timer when the counter eaches 10.

Thanks, JavaAddict. That works exactly for what I need to do. :)

Here you go:

import java.util.Timer;
import java.util.TimerTask;

public class timer {
    public static void main(String[] argv) throws Exception {
        int delay = 5000; // delay for 5 sec.
        int period = 1000; // repeat every sec.
        Timer timer = new Timer();

        TimerTask task = new TimerTask() {
            int count = 0;
            public void run() {
                System.out.println("doing");
                count++;
                if (count == 10){
                    cancel();
                }
            }
        };

        timer.scheduleAtFixedRate(task, delay, period);
    }
}

Hi Doombomber
1. This post is 2 years old, the OP will have long since gone, and
2. Here at DaniWeb we alwyas try to teach people how to write their own code rather than just give an answer they can copy/paste without understanding

Anyway, welcome to DaniWeb.

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.