I'm trying to do a for loop where in it will count the number of times it will be clicked. For example I have a button where i can click that says purchase. Now, from there I want to restrict it by only once. One purchase and that's all he/she cant order or get the freebies again...

i thought of for loop cause it allows to count but then I wonder why on my second click it wont say "You can only avail one freebie for each item. Thank you."

for (int cclick=0; cclick<1; cclick++)
        {
           if (cclick==0)
           {
             globals.executesqlcom("insert into tblorders(cabin,seatnum1,seatnum2,item,price,qty)  VALUES('" + globals.cabin + "','" + globals.seatnum1 + "','" + globals.seatnum2 + "','Travel toothbrush and toothpaste',.00,1" + ")");
             MyMessage.showMessageDialog(this, "Freebies Added to Cart");
            
             cclick = cclick + 1;
           }
        

           else 
           {

               MyMessage.showMessageDialog(this, "You can only avail one freebie for each item. Thank you.");
           }

        }

Dani AI

Generated

Short version: a for-loop inside the click handler will not track clicks across separate mouse events. A local loop/counter is created and discarded each time the method runs, so the logic never "remembers" the previous click. That is why called it effectively non-loop behavior and why warned about tampering with the loop variable. was right to point toward a counter — but that counter must be stored on the object (a field) or enforced in the data layer, not recreated inside the mouse method.

Use a simple boolean or a field-level counter to persist state between clicks. Example pattern (different from the original thread code):

private boolean toothbrushGiven = false;

private void onToothbrushClick(MouseEvent e) {
  if (!toothbrushGiven) {
    // insert row (use PreparedStatement, see note below)
    MyMessage.showMessageDialog(this, "Freebies Added to Cart");
    toothbrushGiven = true;
    toothbrushButton.setEnabled(false); // prevents further UI clicks
  } else {
    MyMessage.showMessageDialog(this, "You can only avail one freebie for each item. Thank you.");
  }
}

Do not rely on UI-only checks for correctness. Always verify on the server/data side before inserting. Example checks (pseudo-SQL):

SELECT COUNT(*) FROM tblorders
 WHERE cabin = ? AND seatnum1 = ? AND item = ?;

-- enforce uniqueness to avoid duplicates due to race conditions
ALTER TABLE tblorders ADD CONSTRAINT uq_seat_item UNIQUE (cabin, seatnum1, item);

Use PreparedStatement to bind parameters (avoid string concatenation), handle SQL exceptions, and catch unique-constraint violations to inform the user gracefully. If multiple different freebies exist, track by item id (Map or Set) rather than a single boolean. Combining a small client-side flag + database checks gives a simple UX and robust data integrity.

Recommended Answers

All 4 Replies

for (int cclick=0; cclick<1; cclick++)
        {
           if (cclick==0)
           {
             globals.executesqlcom("insert into tblorders(cabin,seatnum1,seatnum2,item,price,qty)  VALUES('" + globals.cabin + "','" + globals.seatnum1 + "','" + globals.seatnum2 + "','Travel toothbrush and toothpaste',.00,1" + ")");
             MyMessage.showMessageDialog(this, "Freebies Added to Cart");
            
             cclick = cclick + 1;
           }
        

           else 
           {

               MyMessage.showMessageDialog(this, "You can only avail one freebie for each item. Thank you.");
           }

        }

I don't see how it can make it to the else statement. I also don't see how this is a loop. It's a loop of one iteration, which isn't really a loop. cclick starts at 0, so the if statement on line 3 will be true. cclick will be incremented to 1 on line 8, which ends the loop right there since cclick needs to be less than 1 to remain in the for-loop.

I think everything but lines 5 and 6 can be deleted and you'll have the exact same code.

cclick is a loop variable you may use it as you did in your if statement on line 3. Don't change it as you did on line 8. Changing that variable is normally the responsability of the for statement.

private void toothbrushnpasteMouseClicked(java.awt.event.MouseEvent evt) {

    //if (toothbrushnpaste.addMouseListener(l))

    for (int count=1; count<3; count++)
    {
       if (count==1)
       {
         globals.executesqlcom("insert into tblorders(cabin,seatnum1,seatnum2,item,price,qty)  VALUES('" + globals.cabin + "','" + globals.seatnum1 + "','" + globals.seatnum2 + "','Travel toothbrush and toothpaste',.00,1" + ")");
         MyMessage.showMessageDialog(this, "Freebies Added to Cart");


       }


       else
       {

           MyMessage.showMessageDialog(this, "You can only avail one freebie for each item. Thank you.");
       }

    }
}

hmmm... okay i deleted the one in line 8 and i put it in a mouse event so that when it is clicked twice it should go to the else statement... hmm... i know there's something lacking in here... can you please help me with it? :D

i just dont know how to link the mouse even to the ccount so that it would take effect. :(

What you are wanting to do has absolutely nothing to do with a for() loop. You need a simple if() check on a counter variable that is incremented by your click event - something like

int clickCount=0;

void mouseClicked(){
  ++clickCount;
  if (clickCount > 0){
    // do whatever
  } else {
    // do something else
  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.