1

I am developing an exam software in core java where I am conducting various tests for students.I am stuck at a piece of code where timer with countdown is set. My problem is when I display minutes its working fine but when I try to display seconds with the minutes in mm:ss format its not working.

Code is:

// for below int ti=20,si=60;
// test is for 20 minutes
public void run() {
    while (true) {
        try {
            Thread.currentThread().sleep(60000);

            for (int i = 0; i <= 60; i++) {
                etime.setText("Remaining Time:-" + ti + ":" + si);
                System.out.println("Remaining Time:-" + ti + ":" + si);
                Thread.currentThread().sleep(1000);
                si--;
            }
            ti--;
            if (ti == 0) {
                close();
            }
        } catch (InterruptedException ex) {
            Logger.getLogger(Exam.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}
5
  • Not working in what way? Commented Mar 16, 2013 at 8:23
  • minutes are going down but seconds just showing 60 all the time Commented Mar 16, 2013 at 8:28
  • set si=60 each munites Commented Mar 16, 2013 at 8:28
  • You are sleeping for 1 minute, and then counting down another minute in your loop? Commented Mar 16, 2013 at 8:28
  • you should add another loop for munites Commented Mar 16, 2013 at 8:29

1 Answer 1

1

Once si has counted down to 0, you need to reset it to 59.

Also, the i variable is completely unnecessary, and there is an off-by-one error in your loop.

Sign up to request clarification or add additional context in comments.

1 Comment

In all that excitement, I forgot to actually upvote - done now :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.