this is part of my app where i ask a user if they want to do something. they need to either input a "y" or a "n" for it to do something and if they input something else like a j, 1, 2 , ; , " any other character that isnt "y" or "n" i need it to repeat the questions untill they answer correctly and then to move on with the application.

change1 = JOptionPane.showInputDialog("Change Silo 1 dimensions? (y/n)");
        change2 = JOptionPane.showInputDialog(null,"Change Silo 2 dimensions? (y/n)");
        
 
   
     
              
        if (change1.equalsIgnoreCase("y"))
    {
        do
        {
        String input = JOptionPane.showInputDialog("Enter Radius of Silo:1");
        try
            {
               num = Double.parseDouble(input);
            }   
         catch (NumberFormatException nfe)
            {
                JOptionPane.showMessageDialog(null,"That is not an integer, enter radius again");   
            }
         } while(num < 0 );
         Sone.setRadius(num);
         
        do
        {
        String input = JOptionPane.showInputDialog("Enter height of Silo:1");
        try
            {
               num = Double.parseDouble(input);
            }   
         catch (NumberFormatException nfe)
            {
                JOptionPane.showMessageDialog(null,"That is not an integer, enter height again");   
            }
         } while(num < 0);
         Sone.setHeight(num);
    }
         else if(change1.equalsIgnoreCase("n"))
         {
             System.exit(0);
            
            }
            
                     
        
        if (change2.equalsIgnoreCase("y")) 
   {
        do
        {
        String input = JOptionPane.showInputDialog("Enter Radius of Silo:2");
        try
            {
               num = Double.parseDouble(input);
            }   
         catch (NumberFormatException nfe)
            {
                JOptionPane.showMessageDialog(null,"That is not an integer, enter radius again");   
            }
         } while(num < 0);
         Stwo.setRadius(num);
         
        do
        {
        String input = JOptionPane.showInputDialog("Enter Height of Silo:2");
        try
            {
               num = Double.parseDouble(input);
            }   
         catch (NumberFormatException nfe)
            {
                JOptionPane.showMessageDialog(null,"That is not an integer, enter height again");   
            }
         } while(num < 0);
         Stwo.setHeight(num);
   }
   
   else if(change2.equalsIgnoreCase("n"))
         {
             System.exit(0);
            
            }
   
        if (change1.equalsIgnoreCase("y"))
        {
            JOptionPane.showMessageDialog(null,"Silo 1 Dimensions:\n" + "Radius = " + fmt.format(Sone.getRadius()) +"\nHeight = " + fmt.format(Sone.getHeight()) + "\nVolume = " + fmt.format(Sone.getVolume()) + "\nSurface Area = " + fmt.format(Sone.getSurfaceArea()));
        }
        
        if (change2.equalsIgnoreCase("y"))
        {
            JOptionPane.showMessageDialog(null,"Silo 2 Dimensions:\n" + "Radius = " + fmt.format(Stwo.getRadius()) +"\nHeight = " + fmt.format(Stwo.getHeight()) + "\nVolume = " + fmt.format(Stwo.getVolume()) + "\nSurface Area = " + fmt.format(Stwo.getSurfaceArea()));
        }
    }


}

is this what you want to achieve?

String change1 = "";
while (!change1.equalsIgnoreCase("y") && !change1.equalsIgnoreCase("n")
   change1 = JOptionPane.showInputDialog("Change Silo 1 dimensions? (y/n)");
}
boolean validChoice = false;

while(!validChoice){
//Get the user's input, if it is y or n, set validChoice to true.
}

You said you "need exception", do you mean you want to throw an exception? If so there are a few ways you can do that.

edit: I just realized you're using GUIs to get the input, in which case, James' code is obviously better since it lets the user input through a GUI. There is also a lot of information here showing you how to force the user to input either yes or no (the window won't close until they choose one or the other) and there is an example here of how to put the buttons yes and no on the window. So obviously, combining those two ideas, you can force them to input either yes or no with no loop if you really want to do that.

Or, even better for the user: JOptionPane.showConfirmDialog, which gives yes/no buttons with no risk of user error in the first place. (sorry BJSJC - parallel posted with your reply)

Heh. That invalidates everything I just said about customizing the buttons, I didn't realize they already had one with the buttons yes and no on it. *hangs head* ....

Oh yeah. JOptionPane has a whole library of of useful standard dialog boxes just waiting to go.
That's why we're all here on Daniweb - maybe to help, but also to learn. I guess when there's nothing left to learn you may as well shoot yourself.

String change1 = "";while (!change1.equalsIgnoreCase("y") && !change1.equalsIgnoreCase("n") change1 = JOptionPane.showInputDialog("Change Silo 1 dimensions? (y/n)");}

does this statement say while( change1 is not equal to y and change 1 is not equal to n) repeat change1?

if it is then im pretty sure its what i need :)... thanks guys!

oh and the buttons idea sounds much better but i must use a JOptionPane method.

and yeah i ment i need to throw an exception... like the NumberFormatException.... but isnt there one like StringFormatException where it does the opposite to number one?

anyway if James's code says what i think it says then it should be perfect :)

works perfectly mate. thanks alot again!

im assuming that the code says what i thought it said initially, it works!

another thread solved!! :D

This question has already been answered. Start a new discussion instead.