Results 1 to 12 of 12
- 04-14-2011, 12:33 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 10
- Rep Power
- 0
GregorianCalendar issue, can't use Decision making, help
So I am working through a Java programming book and ran into a little road block. I could just skip it and move on because I know how to solve it with decision making but it is really eating at me. Here is what I have:
import javax.swing.JOptionPane;
import java.util.*;
public class DialogTimer
{
public static void main(String[] args)
{
int time1, time2, milli1, milli2, sec1, sec2, timeDifference;
final int MILLISECSINSECOND = 1000;
GregorianCalendar before = new GregorianCalendar();
milli1 = before.get(GregorianCalendar.MILLISECOND);
sec1 = before.get(GregorianCalendar.SECOND);
time1 = MILLISECSINSECOND * sec1 + milli1;
JOptionPane.showConfirmDialog(null, "Is stealing ever justified? ");
GregorianCalendar after = new GregorianCalendar();
milli2 = after.get(GregorianCalendar.MILLISECOND);
sec2 = after.get(GregorianCalendar.SECOND);
time2 = MILLISECSINSECOND * sec2 + milli2;
timeDifference = time2 - time1;
JOptionPane.showMessageDialog(null, "It took " + timeDifference + " milliseconds for you to answer");
}
}
Now I am supposed to correct the error that happens when time2 is smaller than time1 when the minute rolls over to the next one (resulting in time1 = 58000 and time2 = 1000 for example). The point is to gauge your reaction time to the question using the GregorianCalendar class. I know I am missing something probably pretty obvious, but any help would be appreciated. Again no decision making because that isn't technically until the next chapter.
- 04-14-2011, 12:37 AM #2
Do the instructions say you have to use GregorianCalendar? If not use System.currentTimeMillis method instead.
- 04-14-2011, 01:22 AM #3
Member
- Join Date
- Nov 2010
- Posts
- 10
- Rep Power
- 0
Alright, it said to use the GregorianCalendar class only. I figured it out though....finally. As long as it doesn't take you over and hour to answer (rough I know) or it doesn't go from 1:59 to 2:00 while answering, it should work fine. A lot less room for an error but still possible, best I could do with the limitations. So here it is:
import javax.swing.JOptionPane;
import java.util.*;
public class DialogTimer2
{
public static void main(String[] args)
{
int time1, time2, milli1, milli2, sec1, sec2, min1, min2, timeDifference;
final int MILLISECSINSECOND = 1000, MILLISECSINMINUTE = 60000;
GregorianCalendar before = new GregorianCalendar();
milli1 = before.get(GregorianCalendar.MILLISECOND);
sec1 = before.get(GregorianCalendar.SECOND);
min1 = before.get(GregorianCalendar.MINUTE);
time1 = (MILLISECSINMINUTE * min1) + (MILLISECSINSECOND * sec1) + milli1;
JOptionPane.showConfirmDialog(null, "Is stealing ever justified? ");
GregorianCalendar after = new GregorianCalendar();
milli2 = after.get(GregorianCalendar.MILLISECOND);
sec2 = after.get(GregorianCalendar.SECOND);
min2 = after.get(GregorianCalendar.MINUTE);
time2 = (MILLISECSINMINUTE * min2) + (MILLISECSINSECOND * sec2) + milli2;
timeDifference = (time2 - time1);
JOptionPane.showMessageDialog(null, "It took " + timeDifference + " milliseconds for you to answer");
}
}
It's a good feeling when you finally wrap your head around a seemingly impossible task with the supplies provided :).
- 04-14-2011, 01:33 AM #4
I don't understand why you are bothering with seconds and minutes. If the object is to display how many milliseconds it took why not just use the millisecond values you get from the calendar objects?
- 04-14-2011, 01:57 AM #5
Yeah System.currentTimeMillis() is all you need:
Java Code:long timeBefore = System.currentTimeMillis(); //JOptionPane long timeDiff = System.currentTimeMillis()-timeBefore; //JOptionPane
- 04-14-2011, 01:59 AM #6
@Junky
Actually getting Milliseconds from the calendar object will only get you the number of milliseconds since the last second, not since January 1970.
- 04-14-2011, 01:59 AM #7
- 04-14-2011, 02:03 AM #8
Then they should use the getTimeInMillis method.
- 04-14-2011, 02:07 AM #9
@Junky
Ahah, you're right!
@OP
Because you're only allowed to use GregorianCalendar, you need to use calendar.getTimeInMillis(). It does the same thing as System.currentTimeMillis();
- 04-14-2011, 02:08 AM #10
- 04-14-2011, 02:11 AM #11
Huh?
Your post suggesting to use System.currentTimeMillis is almost 1 and a half hours after I suggested it. Do not pretend you did not see my reply.
- 04-14-2011, 02:17 AM #12
Similar Threads
-
Issue regarding making chart in jsf
By maulikmodi08 in forum JavaServer Faces (JSF)Replies: 0Last Post: 03-10-2011, 04:39 AM -
GregorianCalendar bug?
By wangalex in forum Advanced JavaReplies: 3Last Post: 11-11-2010, 05:24 AM -
c4.5 decision tree
By emailtosridevi in forum SWT / JFaceReplies: 1Last Post: 03-15-2010, 05:34 AM -
GregorianCalendar help
By Sturm in forum New To JavaReplies: 0Last Post: 02-03-2010, 09:43 PM -
Java Looping and decision
By susan in forum AWT / SwingReplies: 1Last Post: 08-07-2007, 04:29 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks