Check java phone number

Here is my problem:

Create a constructor for the phone number specified by a string in the form xxx-xxx-xxxx or xxx-xxxx for the local number. Throw an exception if the format is invalid.

So I thought to test it with a regex, but I don’t know if I am doing this correctly. Also, what exception would I have to throw? Do I need to create my own exception?

    public TelephoneNumber(String aString){
        if(isPhoneNumberValid(aString)==true){
            StringTokenizer tokens = new StringTokenizer("-");
            if(tokens.countTokens()==3){
                areaCode = Integer.parseInt(tokens.nextToken());
                exchangeCode = Integer.parseInt(tokens.nextToken());
                number = Integer.parseInt(tokens.nextToken());
            }
            else if(tokens.countTokens()==2){
                exchangeCode = Integer.parseInt(tokens.nextToken());
                number = Integer.parseInt(tokens.nextToken());
            }
            else{
                //throw an excemption here
            }
        }

    }


 public static boolean isPhoneNumberValid(String phoneNumber){
     boolean isValid = false;

     //Initialize reg ex for phone number.
    String expression = "(\\d{3})(\\[-])(\\d{4})$";
    CharSequence inputStr = phoneNumber;
    Pattern pattern = Pattern.compile(expression);
    Matcher matcher = pattern.matcher(inputStr);
    if(matcher.matches()){
        isValid = true;
     }
        return isValid;
    }
Hi, sorry, yes, this is homework. For these purposes, the only valid format is xxx-xxx-xxxx and xxx-xxxx, all other formats (xxx) xxx-xxxx or xxxxxxxxxx are not allowed in this case.

I would like to know if the correct expression is correct

+5
source share
4

, , , .

. , xxx-xxx-xxxx xxx-xxxx, x , "(\\d{3}-){1,2}\\d{4}". , http://regular-expressions.info.

, ? ?

A ValidatorException .

public static void isPhoneNumberValid(String phoneNumber) throws ValidatorException {
    if (!phoneNumber.matches(regex)) {
        throws ValidatorException("Invalid phone number");
    }
}

, IllegalArgumentException, , , .

, , , / . , .

+7
^(([(]?(\d{2,4})[)]?)|(\d{2,4})|([+1-9]+\d{1,2}))?[-\s]?(\d{2,3})?[-\s]?((\d{7,8})|(\d{3,4}[-\s]\d{3,4}))$

: (0060) 123-12345678, (0060) 12312345678, (832) 123-1234567, (006) 03-12345678,

(006) 03-12345678, 00603-12345678, 0060312345678

0000-123-12345678, 0000-12-12345678, 0000-1212345678... ..

1234-5678, 01-123-4567

'-' SPACE i.e(0080) 123 12345678

+ 82-123-1234567, +82 123 1234567, +800 01 12345678... ..

/ . 1-800-000-0000

* Regex http://regexpal.com/

+3

, BalusC.

StringTokenizer . JavaDoc:

StringTokenizer - , , . , , , split String java.util.regex.

:

String phoneParts[] = phoneNumber.split("-");
0

String pincode = "589877";

    Pattern pattern = Pattern.compile("\\d{6}");

\ d .       Matcher matcher = pattern.matcher(pincode);

    if (matcher.matches()) {
        System.out.println("Pincode is Valid");
        return true;
    } else {
        System.out.println("pincode must be a 6 digit Number");
-3

All Articles