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

    Join Date
    Feb 2013
    Posts
    9
    Rep Power
    0

    Regular expressions and exceptions


    I am trying to validate a phone number and a postal code with regular expressions and using a class exception. I would like to confirm that my regular expression are well done and for my class exception I would like to bring the wrong phone number or postal code (depending on the exception) that the user has enter and a message indicating that is invalid (in the exception class) but I don't how. I will appreciate all the comments and help.

    Customer class code:

    Code:
    public void setPhoneNumber(String phoneNumber) throws InvalidPhoneNumberException
    	{
    		
    		if (phoneNumber=="\\d{3}-\\d{3}-\\d{4}")
    						 
    		{
    			this.phoneNumber=phoneNumber;
    		}
    		else
    			throw new InvalidPhoneNumberException();
    	}
    Code:
    public void setPostalCode(String postalCode) throws InvalidPostalCodeException
    	{
    		 if(postalCode == "/[a-zA-Z][0-9][a-zA-Z](-| |)[0-9][a-zA-Z][0-9]/")
    		 {
    			 this.postalCode=postalCode;
    		 }
    		 throw new  InvalidPostalCodeException()
    		//this.postalCode = postalCode;
    	}
    Class Exception code:

    Code:
    public class InvalidPhoneNumberException extends Exception
    
    {
    	public InvalidPhoneNumberException()
    	{
    		super();
    	}
    	
    	public InvalidPhoneNumberException(String phoneNumber)
    	{
    		super();
    	}
    
    }
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,023
    Rep Power
    1285
    You are comparing strings.

    To create a regular expression and search for it, you'll need to use java.util.regex.Pattern and java.util.regex.Matcher

IMN logo majestic logo threadwatch logo seochat tools logo