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 :)
Code :
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();
}
}
Code :
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();
}
}
}
}
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.
Re: StopWatch resume problem??
Quote:
Originally Posted by
KevinWorkman
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 :)
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?
Re: StopWatch resume problem??
Quote:
Originally Posted by
KevinWorkman
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 :)
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.