#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    2
    Rep 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.

    Code:
    String gloveType ="";
    boolean gloveTypeOK = false;
    
    
    while (! gloveSizeOK)
       {
          try
            {
               gloveSize = JOptionPane.showInputDialog(gloveMsg2);
               if(gloveSize.equals(""))
                   throw new NullPointerException();
                                      
                if(! gloveSize.equalsIgnoreCase("Small") || gloveSize.equalsIgnoreCase("Medium")
                        || gloveSize.equalsIgnoreCase("Large") || gloveSize.equalsIgnoreCase("XLarge"))
                    throw new IllegalArgumentException();
                        gloveSizeOK = true;            
       }// end try
       catch(NullPointerException e)
      {
    System.exit(0);
      } // end catch
    catch(IllegalArgumentException e)
      {
         String errorMsg = String.format("%s is invalid. Enter Small, Medium, Large, XLarge", gloveSize);
         JOptionPane.showMessageDialog(null, errorMsg);
       } //end catch
     } //end while
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    it keeps throwing the IllegalArgumentException
    Is that from inside the big if statement? What is the value of gloveSize that causes the exception to be thrown? The if statement is true for that value.

    Please edit the post and wrap the code in code tags.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0
    NormR Thank you for your reply

    Originally Posted by NormR
    What is the value of gloveSize that causes the exception to be thrown? The if statement is true for that value.
    This statement was enough for to catch my error.

    Here is the fix that I came up with.

    Code:
    if(! (gloveSize.equalsIgnoreCase("Small") || gloveSize.equalsIgnoreCase("Medium")
                        || gloveSize.equalsIgnoreCase("Large") || gloveSize.equalsIgnoreCase("XLarge")))
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Yep.. The properly enclosing ()s fixed it.

IMN logo majestic logo threadwatch logo seochat tools logo