Results 1 to 12 of 12
  1. #1
    ImWithStupid is offline Member
    Join Date
    Nov 2010
    Posts
    10
    Rep Power
    0

    Default 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.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Do the instructions say you have to use GregorianCalendar? If not use System.currentTimeMillis method instead.

  3. #3
    ImWithStupid is offline Member
    Join Date
    Nov 2010
    Posts
    10
    Rep Power
    0

    Default

    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 :).

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    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?

  5. #5
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Yeah System.currentTimeMillis() is all you need:

    Java Code:
    long timeBefore = System.currentTimeMillis();
    //JOptionPane
    long timeDiff = System.currentTimeMillis()-timeBefore;
    //JOptionPane

  6. #6
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    @Junky
    Actually getting Milliseconds from the calendar object will only get you the number of milliseconds since the last second, not since January 1970.

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by ra4king View Post
    Yeah System.currentTimeMillis() is all you need:
    I have already suggested that and OP has stated they must use GregorianCalendar. Please read all post before replying. This will ensure you don't make uselss posts.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Then they should use the getTimeInMillis method.

  9. #9
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    @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();

  10. #10
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    @Junky
    You replied while I was replying so I didn't your posts ;)

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    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.

  12. #12
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

Similar Threads

  1. Issue regarding making chart in jsf
    By maulikmodi08 in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 03-10-2011, 04:39 AM
  2. GregorianCalendar bug?
    By wangalex in forum Advanced Java
    Replies: 3
    Last Post: 11-11-2010, 05:24 AM
  3. c4.5 decision tree
    By emailtosridevi in forum SWT / JFace
    Replies: 1
    Last Post: 03-15-2010, 05:34 AM
  4. GregorianCalendar help
    By Sturm in forum New To Java
    Replies: 0
    Last Post: 02-03-2010, 09:43 PM
  5. Java Looping and decision
    By susan in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 04:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •