In my program I get input from the user (type of topping for a pizza) then if there aren't enough left I have to let them change or cancel that topping.

I do this with a for loop, my question is: how do I make it so it breaks out of the for loop and goes back up to where I get input from the user. Here is a code snippet:

//brings up toppings input dialog
                String smallToppings = 
                JOptionPane.showInputDialog("What kind of toppings do you want on your small pizza?\n\n" +
                                                "We currently have:  pepperoni, mushrooms, meatball, sausage, chicken, and ham.\n\n" + 
                                                "Please enter your toppings with spaces inbetween (ie. pepperoni ham).\n"
                                                + "To check the inventory, type in 'checkINV' without the quotes.", "pepperoni mushroom");
        
                //splits each toppings into an array
                String[] arr = smallToppings.split(" ");
                  
                
                
                //counts how many toppings there are (just by words)
                  for(int k = 0; k <arr.length;k++)
                {//start for
                      smallCount = 0;
                    if(arr[k].equals("")) 
                    {//start if
                        smallCount = 0;
                    }//end if
                    
                    else
                    {//start else
                        smallCount++;
                    //}//end else            
                    
                        //sets arr[j] to nameToFind (String to search for)
                        String nameToFind = arr[k];
                        
                        //sets number of toppings ordered to an array
                        numToppingsOrderedArray[k] = (smallCount * smallPizza);
                        
                        //if found, check to see if there are enough, otherwise we can't make it
                        for(int i = 0; i <toppingsArray.length;i++)
                        {//start for

                            if (nameToFind.equals(toppingsArray[i]))
                            {//start if    
                                
                                //doesn't do anything, just text
                                JOptionPane.showMessageDialog(null,
                                       "Checking to see if there are enough " + toppingsArray[i] + " left...",
                                        "Checking...",
                                        JOptionPane.INFORMATION_MESSAGE);

                                //checks to see if there are enough toppings available
                                if (numToppingsArray[i] >= numToppingsOrderedArray[k])
                                {//start if
                                        JOptionPane.showMessageDialog(null,
                                               "There are enough " + toppingsArray[i] + " to make your pizza.",
                                                    "Success!",
                                                    JOptionPane.INFORMATION_MESSAGE);
                                    
                                    //removes number of toppings ordered from those on hand
                                    numToppingsArray[i] = numToppingsArray[i] - numToppingsOrderedArray[k];
                                    
                                }//end if
                            

                                else
                                {//start else
                                    notAvailable = JOptionPane.showConfirmDialog
                                    (null , "There aren't enough " + toppingsArray[i] + " toppings left." +
                                            "\nWould like you to order something else?"
                                    , "Pizza Palace" , JOptionPane.YES_NO_OPTION);
                                    
                                    if(notAvailable == JOptionPane.YES_OPTION)
                                    {//start if
                                        smallCount = 0;
                                    
                                        //something goes here...
                                        
                                    }//end if
                                    
                                    if(notAvailable == JOptionPane.NO_OPTION)
                                    {//start if
                                        smallCount = 0;
                                        
                                        
                                    }//end if
                                }//end else
                              }//end if

it would be alot better if you would have planned this into the method before hand but the easiest way to do this that i can see is to wrap the entire code with :

boolean okFlag = false;
while (!okFlag) {
...CODE HERE
}

then when you want to break from this i.e. if the toppings arent there you can call

break;

this would stop the for loop and return to the outer while loop. In which it would call the top two line again and the rest of the method.

when wanting to exit this loop simply change the boolean value 'okFlag' to true.

Im also pretty much a newb so if this is totally wrong go easy :D

That seems to have worked. Thanks a lot.

p.s. In your signature, you need to fix the links. They both have ] in the hyperlink, so when you click it the url doesn't work.

Well, that works if the customer orders 1 topping -- but it doesn't work if the customer orders two toppings. It's not breaking out of the for loop until it's done with it iteration. Any ideas on how to fix that? I haven't been able to fix it.

Here is the updated snippet of code:

EDIT: got it working...break labels are pretty helpful :)

That seems to have worked. Thanks a lot.

p.s. In your signature, you need to fix the links. They both have ] in the hyperlink, so when you click it the url doesn't work.

why thank you :D

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.