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.");
           }

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