Results 1 to 8 of 8
  1. #1
    Java-learner is offline Member
    Join Date
    Sep 2008
    Posts
    4
    Rep Power
    0

    Default Exception Handling for Letters/Alphabets

    Hello,

    Excuse me if the question sounds silly: about - exception handling.

    I understand that there is "NumberFormatException" in order to catch exceptions for integers. Is there something like that available for letters or alphabets? I mean, if in an integer field if I enter an alphabet I can catch this with :

    int value;
    try {
    System.out.println ("Enter a number")
    value= new Integer(keyboardInput.readLine()).intValue();
    }
    catch (NumberFormatException nfe)
    {
    System.out.println("\n That was not an integer, Please try again.");
    }

    Is there something like this available to catch when a number is entered for a name fields or if it is left blank.

    I appreciate any suggestions.

    Regards,

    JavaLearner.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    Yes, but not from Sun. There are listeners that you can use to examine and reject input for the text input classes. You have to provide your own editing code.

  3. #3
    Java-learner is offline Member
    Join Date
    Sep 2008
    Posts
    4
    Rep Power
    0

    Default

    Thanks for the reply Norm.

    Quote Originally Posted by Norm View Post
    Yes, but not from Sun. There are listeners that you can use to examine and reject input for the text input classes. You have to provide your own editing code.
    Did you mean something like this for checking letters, or is it :confused: childish:

    if ((strName == "") || (strName == null) || ((Pattern.compile("\\s*[0-9]*").matcher( strName ).matches())))
    throw new IOException();

    do { count = 0;
    catch (IOException e)
    {System.out.println("Wrong input. Enter a letter (a-z)");
    count = 1;}
    while count ==1;

    Rgds,
    Java-learner

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    That would be how you do an edit.
    I was talking about trapping the input as it was entered. The edit could be done as it was entered or afterwards. Up to you.

  5. #5
    Java-learner is offline Member
    Join Date
    Sep 2008
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    I was talking about trapping the input as it was entered. .
    Please enlighten me. This is what I guess, I was looking for. But no clue how to trap.

    Any examples?

    Rgds,

    Java-learner

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    Have you looked at the listeners for the text input areas?
    Also there are document listeners for the document associated with JTextArea. It has a listener.

    Do some searches for coding examples. It'll take some looking at to see how its done.

  7. #7
    Java-learner is offline Member
    Join Date
    Sep 2008
    Posts
    4
    Rep Power
    0

    Default

    I am not using any GUI/ Swing. Just in DOS prompt.

    Anyhow, thanks for your advice. I will go check on that.

    Thanks,

    Java-learner.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    That makes it a lot simpler.
    Basically you want to scan a String for a numeric. A regex will handle that.
    Your above code seems to do that already.

Similar Threads

  1. Exception handling and logging
    By jurka in forum New To Java
    Replies: 8
    Last Post: 09-03-2008, 07:07 PM
  2. JAXP exception handling
    By jovenky in forum Advanced Java
    Replies: 0
    Last Post: 05-27-2008, 01:37 PM
  3. Exception Handling...
    By focus_nitin in forum New To Java
    Replies: 1
    Last Post: 02-16-2008, 03:13 AM
  4. Jstl Exception Handling
    By vamsidharpoosarla in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 07-18-2007, 06:17 AM
  5. JSTL Exception Handling
    By chaatf in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-18-2007, 02:24 AM

Posting Permissions

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