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

Thread: Problem with timer

  1. #1
    Member Hikaros's Avatar
    Join Date
    Sep 2013
    Posts
    42
    My Mood
    Love
    Thanks
    10
    Thanked 2 Times in 2 Posts

    Default Problem with timer

    hi, this application came to my mind to see how long would it take to type the string "que" (the purpose was a joke but seeing how it isn't behaving how i thought it should i decided to post it here to learn why)

    this is the Gui Class

    import javax.swing.*;
     
    public class Gui extends JFrame {
     
        private JTextField field;
        private JTextArea area;
        private int sec=0;
        private Long currentTime = 0L;
        private Long elapsedTime = 0L;
        private String s;
     
     
        public Gui(){
            super("Word typing timer");
            setLayout(null);
     
            field = new JTextField("0");
            field.setBounds(10,10,200,30);
            field.setEditable(false);
            add(field);
     
            area = new JTextArea();
            area.setBounds(10,50,400,300);
            add(area);
     
            s = area.getText().trim();
        }
     
        public void tempori(){
            try{
                currentTime = System.currentTimeMillis();
     
                while(true){
     
                    area.requestFocus();
     
                    while(!s.equalsIgnoreCase("que")){
                        if (!s.equalsIgnoreCase("")){
     
                            elapsedTime = System.currentTimeMillis() - currentTime;
     
                            if (elapsedTime==1000){
                            elapsedTime=0L;
                            sec+=1;
                            }
     
                            field.setText(String.format("%d:%d",sec,elapsedTime));
                        }
                        s = area.getText().trim();
                    }
                }
            }catch(Exception ex){
                System.out.println(ex.getMessage());
            }
        }
     
    }

    This is the main

    import javax.swing.*;
     
    public class Main {
        public static void main(String[] args){
            Gui form = new Gui();
            form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            form.setBounds(0,0,540,400);
            form.setLocationRelativeTo(null);
            form.setVisible(true);
     
            try{
                form.tempori();
            }catch(Exception ex){
                ex.printStackTrace();
            }
     
        }
     
    }

    What the code should do:

    if the JTextArea is equals to nothing or the string "que "then stop the timer process and if not then to continue.

    I have several problems:

    1- sometimes the program just won't run, throwing a "null" exception for who knows what reason... i need to close it and re-run it for it to work, i have swear i have no idea why it does that.

    2-
                            if (elapsedTime==1000){
                                elapsedTime=0L;
                                sec+=1;
                            }

    it just doesn't do it, it never resets to 0L. if i change from == to >=1000 then the entire format goes to hell and the sec variable in the JTextField goes crazy.

    3- if i change the line currentTime = System.currentTimeMillis(); to be right below area.requestFocus(); the sec variable literally jumps to a random number.

    I have tried to debug it but i swear i have no idea what is going on. Can someone tell me what is the error?


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: Problem with timer

    Remember it is milliseconds, so 1000 is actually 1 second. Therefore I doubt elapsed time will ever be equal to 1 second.
    Improving the world one idiot at a time!

  3. #3
    Member Hikaros's Avatar
    Join Date
    Sep 2013
    Posts
    42
    My Mood
    Love
    Thanks
    10
    Thanked 2 Times in 2 Posts

    Default Re: Problem with timer

    Yeah, that's why i have that after elapsedTime is equal to 1000 to reset it and count 1 to sec variable but it just doesn't do it lol

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: Problem with timer

    Maybe I misunderstand you or maybe you misunderstand me.

    The point I was trying to make was you do something and calculate how long it took. If and only if that took 1 second then reset it back to 0. What do you think are the odds that whatever always takes EXACTLY 1 second to complete?
    Improving the world one idiot at a time!

  5. #5
    Member Hikaros's Avatar
    Join Date
    Sep 2013
    Posts
    42
    My Mood
    Love
    Thanks
    10
    Thanked 2 Times in 2 Posts

    Default Re: Problem with timer

    ah well about that according to some things i read about calculating like that it isn't always exact since there are numeric errors, but every language is like tha if i'm not mistaken, however using the currentTimeMillis() should be accurate enough haha i don't really need it to have dead exact accuracy i just don't know why it isn't behaving the way it should.

    maybe i'm not explaining myself right. so i will do it in a different way:

    public void tempori(){
            try{
                currentTime = System.currentTimeMillis();
     
                while(true){
     
                    area.requestFocus();
     
                    while(!s.equalsIgnoreCase("que")){
                        if (!s.equalsIgnoreCase("")){
     
                            elapsedTime = System.currentTimeMillis() - currentTime;
     
                            if (elapsedTime==1000){
                            elapsedTime=0L;
                            sec+=1;
                            }
     
                            field.setText(String.format("%d:%d",sec,elapsedTime));
                        }
                        s = area.getText().trim();
                    }
                }
            }catch(Exception ex){
                System.out.println(ex.getMessage());
            }
        }

    i get the total time at that moment in currentTime, enter the infinite loop and then check for the conditions to start the calculations

    elapsedTime = System.currentTimeMillis() - currentTime;

    when that happens it shouldn't be over 1000 so it should be that at one point and enter the If statement but it never does, i changed the if statement just in case it went over 1000 due to the numeric error when looping for it to be >= instead of == but if i do that, the sec variable goes wild.

    on the other problems which i seriously don't understand is that sometimes (a lot of the times) when i run the program and i type anything, no matter what it is, the throws a null exception and the program just doesn't work anymore so i have to stop it and re-run it, sometimes it does it again and sometimes it doesn't, i'd like help on that as well.

Similar Threads

  1. Shutdown Timer
    By 0w1 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 26th, 2013, 10:53 AM
  2. Timer.schedule
    By johnboyofsj in forum Java Theory & Questions
    Replies: 1
    Last Post: October 14th, 2012, 10:02 PM
  3. Thread/timer problem
    By korbal in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 18th, 2010, 05:59 PM
  4. bubble sort timer problem
    By JavaNoob82 in forum Algorithms & Recursion
    Replies: 1
    Last Post: March 12th, 2010, 08:22 AM
  5. Timer?
    By TimW in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 27th, 2009, 07:43 AM