Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 692233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:37:46+00:00 2026-05-14T02:37:46+00:00

Here is my problem: Create a constructor for a telephone number given a string

  • 0

Here is my problem:

Create a constructor for a telephone number given a string in the form xxx-xxx-xxxx or xxx-xxxx for a local number. Throw an exception if the format is not valid.

So I was thinking to validate it using a regular expression, but I don’t know if I’m doing it correctly. Also what kind of 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 this assignments the only valid format are xxx-xxx-xxxx and xxx-xxxx, all other formats (xxx)xxx-xxxx or xxxxxxxxxx are invalid in this case.

I would like to know if my regular expression is correct

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:37:47+00:00Added an answer on May 14, 2026 at 2:37 am

    So I was thinking to validate it using a regular expression, but I don’t know if I’m doing it correctly.

    It indeed looks overcomplicated. Also, matching xxx-xxx-xxxx or xxx-xxxx where x is a digit can be done better with "(\\d{3}-){1,2}\\d{4}". To learn more about regex I recommend to go through http://regular-expressions.info.

    Also what kind of exception would I have to throw? Do I need to create my own exception?

    A ValidatorException seems straight forward.

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

    If you don’t want to create one yourself for some reasons, then I’d probably pick IllegalArgumentException, but still, I don’t recommend that.

    That said, this validation of course doesn’t cover international and/or external telephone numbers. Unless this is really homework, I’d suggest to rethink the validation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my problem. I want to create n number of QLineEdit widgets (
OK, here is how to re-create the problem had: Create a new project, using
I am trying to create a simple drawing game like DrawSomething. The problem here
I'm having a hard time trying to create a XmlRepository. The problem here I
Here is the problem details: 1) I want to create dynamic (ip based) download
Here's my problem: I have do create a menu/list of actions (which would be
My problem : I need to create draggable widgets (here it's a jslider for
Here is the problem I want to use a constructor that will simplify image
So here's my problem: I've created a custom AVCaptureSession that takes pictures. I'm not
I've created an example of the problem here: http://jsfiddle.net/JustinN/qWeLT/1/ My actual code is hooked

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.