Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Exception handling for TextFields

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Exception handling for TextFields

    Hey guys! This is my first post and its about an issue im having its driving me nuts and im sure its very simple yet i cannot figure it out! Here's the thing, i need to validate a textfield using Exceptions. Here are the validations:

    • If the textfield contains a number, catch the exception and warn an error message
    • If the text field contains an invalid character other than letters ( @#!?_, etc... ) , catch an exception and show an error message.


    The other validations i pretty much managed to do them but those on the list i just cant seem to. I did manage to the opposite, which is "If the textfield contains a non-numeric character, catch the exception and show an error message". I did it like this:

    try{
    Integer.parseInt(jtextField.getText() );
    }
     
    catch ( NumberFormatException e ) {
    JOptionPane.showMessageDialog(null, "The value must be a numeric value. " );
    }

    However i dont know how to do the opposite of that ( which is what i listed ). Any help please?

    Thanks in advance.


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Exception handling for TextFields

    First of all I would like to welcome to you to Java Prgoramming Forums. I hope we can help you out

    Well here is an example that should get you thinking along the right lines and probably give you a solution. It's not using AWT or anything just commandline for ease. But you can implement it into your example easy enough.

    public class FretDancer {
     
    	public void checkForInvalidCharacter(String s) 
    			throws CustomFormatException {
    		for(int i = 0; i < s.length(); i++) 
    			if(!Character.isLetterOrDigit(s.charAt(i)))
    				throw new CustomFormatException("Invalid Character");
    	}
     
    	public FretDancer(){
    		String testString = "Hello,";
     
    		try {
    			checkForInvalidCharacter(testString);
    		} catch (CustomFormatException cfe) { cfe.printStackTrace(); }
    	}
     
    	public static void main(String[] args) {
    		new FretDancer();
    	}
    }
     
    class CustomFormatException extends java.lang.Exception {
    	public CustomFormatException(String s){
    		super(s);
    	}
    }

    Just a note instead of using your own custom exception you could use IllegalArgumentException which would be used in the same way, but you wouldn't need the custom exception class.

    Sorry for the slow reply.

    Regards,
    Chris
    Last edited by Freaky Chris; June 16th, 2009 at 08:33 AM.

Similar Threads

  1. exception while Read very large file > 300 MB
    By ps.ganesh in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: June 11th, 2009, 11:39 PM
  2. Getting Null Pointer Exception in runtime
    By RoadRunner in forum Exceptions
    Replies: 1
    Last Post: April 26th, 2009, 01:21 PM
  3. Exception handling
    By AnithaBabu1 in forum Exceptions
    Replies: 6
    Last Post: August 27th, 2008, 09:37 AM