Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Can't Get Timer to Work, What Am I doing Wrong?!?!

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can't Get Timer to Work, What Am I doing Wrong?!?!

    I'm trying to make a program where you enter the amount of minutes you want to time and when it's done it will tell you the timer is done. Here's the code

    import javax.swing.JOptionPane;
     
    //1 minute = 60,000 milliseconds
     
    public class Main {
    	String input;
    	long timer;
     
    	long startTime;
    	long currentTime;
    	long endTime;
     
     
    	public void start(){
     
    			input = JOptionPane.showInputDialog("Enter the amount of minutes you want to time");
    			timer = Long.parseLong(input);
    			timer = timer*60000;
     
    			startTime = System.currentTimeMillis();
     
     
    			System.out.println(startTime);
     
    			timer();
     
    	}
     
    	public void timer(){
    		while(true){
    			currentTime = System.currentTimeMillis();
    			endTime = startTime+timer;
     
    			startTime++;
     
    			if(currentTime == endTime){
    				takeBreak();
    			}
    		}
    	}
     
    	public void takeBreak(){
    		JOptionPane.showMessageDialog(null, "Timer Finished");
    		System.exit(0);
    	}
     
    	public static void main(String[] args){
    		Main main = new Main();
    		main.start();
     
     
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can't Get Timer to Work, What Am I doing Wrong?!?!

    What happens when you execute the code?
    Can you explain the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can't Get Timer to Work, What Am I doing Wrong?!?!

    When I run it the timer never ends, is my code good?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can't Get Timer to Work, What Am I doing Wrong?!?!

    is my code good?
    You need to try debugging the code to see what it is doing. Add a println statement inside the while loop that prints out the values of currentTime and endTime just before the if statement so you can see what their values are as the code executes.
    For testing:
    I suggest you remove the *60000 and just use the value entered for the duration to make the numbers more reasonable.
    Also I recommend that you replace the while(true) loop with a for loop that goes for 100 loops.
    That will be enough times that the print out will show you what the problem is.

    Another suggestion: Don't use an == test for the end time,
    better to use >=


    BTW This is the absolute worst way to have a program wait.
    The Thread class's sleep() method is one better way. Later you find some other better ways.

    I'm Done for tonight. Back tomorrow.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Timer.schedule
    By johnboyofsj in forum Java Theory & Questions
    Replies: 1
    Last Post: October 14th, 2012, 10:02 PM
  2. Timer code does not work!
    By mariapatrawala in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 10th, 2010, 10:03 AM
  3. my menu doesnt work can u tell me whats wrong
    By claymore in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 8th, 2010, 04:16 AM
  4. Timer?
    By TimW in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 27th, 2009, 07:43 AM
  5. How to Use Timer in Java
    By neo_2010 in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: September 1st, 2009, 12:10 PM