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 6 of 6

Thread: StopWatch resume problem??

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default StopWatch resume problem??

    Hey i stayed up all night trying to figure this out, so im asking you to help me solve this problem so I can get back to sleep

    The problem: I've created a simple console stopwatch program. When i press ENTER the watch starts running from time 0:0:0, next time i press ENTER the time that has elapsed is printed, for example 0:0:10 (10 seconds).

    BUT, when i press ENTER again i want the stopwatch to resume on the previous time! This should be a simple logical problem but im blind for the moment so really nice if you could help me out



    import java.io.IOException;
    import java.util.Date;
     
    public class Main {
     
    	public static Date timeStampFirst = new Date();
     
    	public static void main(String[] args) {
     
    		Timer timer = new Timer();
    		Thread t1 = new Thread(timer);
    		t1.start();
     
    	}
     
    }









    import java.io.IOException;
    import java.util.Calendar;
    import java.util.Date;
     
    public class Timer implements Runnable {
     
    	private boolean isRunning = false;
     
    	long elapsedTime = 0;
    	long tempTime = 0;
     
    	@Override
    	public void run() {
     
    		isRunning = true;
    		long previousTime = 0;
     
    		while (isRunning) {
     
    			try {
     
    				System.out.println("Press any key to start/stop stopwatch");
     
    				if (System.in.read() != -1) {
    					Date timeStamp = new Date();
    					if (tempTime == 0) {
     
    						tempTime = timeStamp.getTime();
    						Main.timeStampFirst.setTime(tempTime);
    					}
    					Main.timeStampFirst.setTime(timeStamp.getTime() - elapsedTime);
    					System.out.println("Running...");
    				}
     
    				if (System.in.read() != -1) {
     
    					System.out.println("Stopped!");
    					Date timeStampStop = new Date();
    					elapsedTime = timeStampStop.getTime() - Main.timeStampFirst.getTime() - 3600000;
    					previousTime = elapsedTime;
    					Date finishTime = new Date();					
    					finishTime.setTime(elapsedTime);
    					System.out.println("Time elapsed: " + finishTime.getHours() + ":" + finishTime.getMinutes() + ":" + finishTime.getSeconds());
    					timeStampStop.setTime(0); 
    					finishTime.setTime(0);
    				}
     
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
     
     
    		}
     
    	}
     
    }
    Last edited by Tobbe129; January 19th, 2011 at 07:53 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,301
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: StopWatch resume problem??

    When posting code, make sure it's in SSCCE (that's a link) form, and make sure you use the code tags. Nobody likes to read unformatted code.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: StopWatch resume problem??

    Quote Originally Posted by KevinWorkman View Post
    When posting code, make sure it's in SSCCE (that's a link) form, and make sure you use the code tags. Nobody likes to read unformatted code.
    Done

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,301
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: StopWatch resume problem??

    Why are you subtracting 3600000 from the elapsedTime?

    What is the point of previousTime?

    Why are you "starting" and "stopping" for every key stroke? Shouldn't they be alternating?

    Why do you use deprecated methods?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: StopWatch resume problem??

    Quote Originally Posted by KevinWorkman View Post
    Why are you subtracting 3600000 from the elapsedTime?

    What is the point of previousTime?

    Why are you "starting" and "stopping" for every key stroke? Shouldn't they be alternating?

    Why do you use deprecated methods?
    3600000 is an hour in milliesecounds, when i create a Date object and sets it to 0, it becomes 1970 + an hour, so like 01:00:00, so when i take away 3600000 it becomes 0

    Why not shouldn't it work? My teacher didn't say anything about it, when you hit ENTER it starts, when you do it again it stops, could that be the problem? Hmmm

    I use deprecated methods because this is just an exercise, I know i shouldnt

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,301
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: StopWatch resume problem??

    But you aren't dealing with setting it to zero, you're dealing with setting it to an elapsed time. Hint- what happens when the elapsed time is less than an hour (which will usually be the case)?

    But I think your main problem is that you're "starting the clock" and then immediately "stopping" it. You aren't waiting for a second keystroke. You're doing both actions each time. That sort of pre-empts any other logical errors you might have.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!