Results 1 to 5 of 5
  1. #1
    Scyth3 is offline Member
    Join Date
    Nov 2012
    Posts
    12
    Rep Power
    0

    Arrow while loop is looping twice

    Fisrt of all, I am making an auto typer/ clicker... this error is on the clicker part. Here is my code.

    Java Code:
        public static void main(String[] args) throws AWTException{
            r = new Robot();
            Frame.interval = Float.parseFloat(Frame.t2.getText());
            Frame.duration = (1 / Frame.interval) * (Float.parseFloat(Frame.t3.getText())) * 60;
    
            while (Frame.duration > 0) {
                
                    r.mousePress(InputEvent.BUTTON1_DOWN_MASK);
                    r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
                    Frame.duration--;
                    Frame.wait(Frame.interval);
                    System.out.println(Frame.duration);
    
    
            }
    
        }
    With the interval set at 1 second, and the duration set at 0.25 minutes, this is the output I get.

    Java Code:
    14.0
    13.0
    12.0
    11.0
    10.0
    9.0
    8.0
    7.0
    6.0
    5.0
    4.0
    3.0
    2.0
    1.0
    0.0
    14.0
    13.0
    12.0
    11.0
    10.0
    9.0
    8.0
    7.0
    6.0
    5.0
    4.0
    3.0
    2.0
    1.0
    0.0
    As you can see, it should click 15 times, but clicks 30.
    If you can help me with code, or simply explain what is going on, please do.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: while loop is looping twice

    Have you check the what is the value of Frame.duration in the while-loop expression?

  3. #3
    Scyth3 is offline Member
    Join Date
    Nov 2012
    Posts
    12
    Rep Power
    0

    Default Re: while loop is looping twice

    thats what the 30 lines of output is

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: while loop is looping twice

    After it goes to 0.0 at line 15 what changes its value to 14.0? I'd expect its value to go to -1?
    When is that change made? Could it happen between lines 12 and 15?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

    Default Re: while loop is looping twice

    A GUI class with static fields smacks of OOPS denial. And an isolated, uncompilable main(...) method with a bunch of code that should never be in any static method doesn't show up a problem.

    To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem. Be prepared for constructive criticism.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Looping: Help Please
    By michelle_323 in forum New To Java
    Replies: 5
    Last Post: 10-12-2011, 11:59 AM
  2. Looping a loop of loops
    By Taikei_no_Yuurei in forum New To Java
    Replies: 2
    Last Post: 09-08-2011, 05:35 AM
  3. While loop not looping need help
    By MichaelT in forum New To Java
    Replies: 5
    Last Post: 03-06-2011, 09:26 PM
  4. Looping
    By Dean29126 in forum New To Java
    Replies: 3
    Last Post: 09-08-2010, 02:01 PM
  5. Help with While and For Looping
    By gmoney8316 in forum New To Java
    Replies: 2
    Last Post: 03-03-2010, 10:54 PM

Posting Permissions

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