This discussion is archived
8 Replies Latest reply: Jun 10, 2007 7:17 AM by 807600 RSS

What exception covers numbers being input when it shoud be a string?

807600 Newbie
Currently Being Moderated
Hi, I am having problems with one of my catch clauses, (Exception e) isnt picking up numbers being input istead of letters any help would be great

Thanks
private void personalDetailsButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                      
boolean stop=false;
     Inductee.setTrainingGoal((String) trainingGoalComboBox.getSelectedItem());
     try{
      Inductee.setName(nameText.getText());
     }
     catch (Exception e) {
         membershipNoText.setText("");
           stop=true;
        }
     
     String age=((String) ageComboBox.getSelectedItem());
    
     try{
     Inductee.setMembershipNo(membershipNo=Integer.parseInt(membershipNoText.getText()));
     }
     catch (Exception e) {
           membershipNoText.setText("");
           stop=true;
        }
     
     Inductee.setTrainerProfile((String) trainerProfileComboBox.getSelectedItem());
     int ages=Integer.parseInt(age);
     Inductee.setAge(ages);
     if(stop==true)
     {
     JOptionPane.showMessageDialog(null, "YOU MUST ENTER CHARACTERS FOR MEMBERS NAME OR NUMBERS FOR MEMBERS MEMBERSHIP NUMBER");
      membershipNoText.requestFocus();
     }else{
     this.dispose();
     new MedicalScreen().setVisible(true);
     }
     
    }                                         
  • 1. Re: What exception covers numbers being input when it shoud be a string?
    3004 Newbie
    Currently Being Moderated
    It's not clear what problem you're having. Please explain what input you provided, what behavior you expected, and what you saw instead. Be very specific.
  • 2. Re: What exception covers numbers being input when it shoud be a string?
    807600 Newbie
    Currently Being Moderated
    Hi, when the user is asked to enter their name into the textfield, I would like to catch if the user has entered numbers insted of letters.

    when running my program I can type numbers into the textfield and no error message will appear.

    At the bottom of my code I have a dialog box that appears if numbers instaed of letters have been input for the users name and letters instead of numbers for the users membership number, as it stands at the moment the program is only catching the user membership clause and not the name clause

    Thanks
  • 3. Re: What exception covers numbers being input when it shoud be a string?
    3004 Newbie
    Currently Being Moderated
    What exact input did you provide?

    What exactly happened? Which code was executed, what were the results, etc.?

    What exactly did you expect to happen? Which code did you expect to execute, what results did you expect, etc.?
  • 4. Re: What exception covers numbers being input when it shoud be a string?
    807600 Newbie
    Currently Being Moderated
    Hi, hope this helps

    What exact input did you provide?
    The numbers 123

    What exactly happened?
    When I clicked my action button the gui went to the next screen with no JOptionDialog appearing.

    Which code was executed?
    The action button code was executed and no dialogbox appeared on screen

    what were the results, etc.?
    The catch clause didnt detect that 123 as a number

    Thanks
  • 5. Re: What exception covers numbers being input when it shoud be a string?
    3004 Newbie
    Currently Being Moderated
    If I understand correctly, you're using this to parse the int. If it fails to parse, all is well, but if it parses, then they entered a number, so the "letters only" dialog should appear.
    try{
         Inductee.setMembershipNo(membershipNo=Integer.parseInt(membershipNoText.getText()));
         }
         catch (Exception e) {
               membershipNoText.setText("");
               stop=true;
            }
    Put some print statements in or use a debugger so you can see exactly what's going on, exactly which code is being executed, what parseInt is doing, etc.

    Also, what if they enter "abc123"? That has numbers, but parseInt will fail. If you want it to contain only letters, then something like matches("\\p{Alpha}+") is how I'd go.
  • 6. Re: What exception covers numbers being input when it shoud be a string?
    807600 Newbie
    Currently Being Moderated
    Thanks will give that a go

    Cheers
  • 7. Re: What exception covers numbers being input when it shoud be a string?
    807600 Newbie
    Currently Being Moderated
    On one note i would like to say that you sure do seem to have a lot of problems.

    I think i have a solution for how you could solve this problem, what i am not sure of is how efficient it is. Also i am unsure of how to implement it into your code, so I will leave that to you. I have explained things to the very best of my ability, to help you.

    *Obtain the string.
    *Covert it to a char array using String.toCharArray();
    *use a for loop to iterate through the array, and in each iteration call Character.isDigit() on the character and check if the value returned is true.
    *If the number is true, then you know a number has been entered                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
  • 8. Re: What exception covers numbers being input when it shoud be a string?
    807600 Newbie
    Currently Being Moderated
    Also, after reviewing your code once again i saw something.
    You expected it to give you an error for getting the text in a field.
    But numbers are also counted as text ( as numbers can be put in strings)
    Therefore the there would be no exception.

    Hope that helped, next time do remember that numbers are also text