Results 1 to 3 of 3
  1. #1
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Question [SOLVED] Sybtax for Try Catch for empty textfield

    I have the try catches working in my program such as

    Java Code:
    try {
                  aGender = txtGender.getText();  //read aGender from the screen as a string
    
               if  ((aGender.equals("M")) || (aGender.equals("m")) || (aGender.equals("F")) || (aGender.equals("f")))
    // if string is M, m, F or f then set dummy value to OK
                    OK = "ok";
               else
                    throw new IllegalArgumentException("code must be M, m, F or f");
    //  otherwise trap the error
                  }
    
                        catch (IllegalArgumentException e)   {   // catch the error
                            optPane1.showMessageDialog(this,e.getMessage(),"please re-enter",optPane1.ERROR_MESSAGE);
                  }
    However I now need one for another field

    to detect and throw an exception if the user does not put anything in the field, I seem to be chasing my tail. Tried allsorts but I either get it so you get the exception no matter what you enter or dont enter or it just doent work full stop.

    Last one I tried which didt make much sense but I am trying anything now is

    Java Code:
    try {
                  aMembership = txtMembership.getText();  //read aMembership from the screen as a string
    
               if  ((aMembership .equals(" "))) 
    // if entry is made then set dummy value to OK
                    OK = "ok";
               else
                    throw new IllegalArgumentException("Enter Membership Number");
    //  otherwise trap the error
                  }
    
                        catch (IllegalArgumentException e)   {   // catch the error
                            optPane1.showMessageDialog(this,e.getMessage(),"please re-enter",optPane1.ERROR_MESSAGE);
                  }
    Where am I going wrong, as I say the user must enter some data in this field to get passed this try catch.

    Thanks in advance.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    have you tried the String method isEmpty()? That's the one I'd try here. Again I advise you review the String API both for API practice and to see what methods String has available.

  3. #3
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Talking

    Thanks Fubarable, just needed some logical thinking

    Java Code:
    try {
                  aMembership = txtMembership.getText();  //read aMembership from the screen as a string
    
               if  ((aMembership.isEmpty()))
    // if entry is made then throw error
                   
                    throw new IllegalArgumentException("Enter Membership Number");
    //  otherwise trap the error
                  }
                        catch (IllegalArgumentException e)   {   // catch the error
                            optPane1.showMessageDialog(this,e.getMessage(),"please re-enter",optPane1.ERROR_MESSAGE);
                  }
    Works great

    Thanks again

Similar Threads

  1. empty arrays.
    By circuspeanuts in forum New To Java
    Replies: 11
    Last Post: 04-06-2009, 08:08 PM
  2. how to catch two exceptions in one catch()?
    By arnab321 in forum New To Java
    Replies: 1
    Last Post: 11-06-2008, 11:54 AM
  3. KeyBindings Empty Key?
    By Unome in forum Java Applets
    Replies: 1
    Last Post: 10-24-2008, 08:28 PM
  4. Empty ResultSet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-09-2008, 09:36 PM
  5. BufferedReader empty
    By Peter in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2007, 07:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •