November 2nd, 2013, 03:47 AM
Join Date: Nov 2013
Time spent in forums: 19 m 24 sec
Reputation Power: 0
While loop with if..else and exceptions
I am having trouble getting some code to work properly if anyone could help me I would appreciate it.
I am looking for this to display a window to collect user input.
Currently it will work if I enter "small" but it keeps throwing the IllegalArgumentException and goes back to the question as it should with any other input. So what I am looking for is to be able to store small, medium, large or xlarge in the gloveSize variable depening on which option the user inputs.
I am sure this is not the best way to right this code, however this is the requirement for the project right now.
String gloveType ="";
boolean gloveTypeOK = false;
while (! gloveSizeOK)
gloveSize = JOptionPane.showInputDialog(gloveMsg2);
throw new NullPointerException();
if(! gloveSize.equalsIgnoreCase("Small") || gloveSize.equalsIgnoreCase("Medium")
|| gloveSize.equalsIgnoreCase("Large") || gloveSize.equalsIgnoreCase("XLarge"))
throw new IllegalArgumentException();
gloveSizeOK = true;
}// end try
} // end catch
String errorMsg = String.format("%s is invalid. Enter Small, Medium, Large, XLarge", gloveSize);
} //end catch
} //end while