Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The following answer is on Chegg, but is not being accepted by the Programming L

ID: 3597465 • Letter: T

Question

The following answer is on Chegg, but is not being accepted by the Programming Lab. Are there other possibilities? The error message is protesting the format, I think: Remarks:           Problem: (([2-9]d{2})||[2-9]d{2})(.d{3}.d{4}||-d{3}-d{4})(s[xX]d{1,4}||[xX]d{1,4}){0,1} accepted: 604-83-5020 I'm guessing the problem is that it accepts two middle digits instead of three. The xxx-xx-xxxx format is that of a social security number, not a phone number. Write a regular expression that selects lines containing a phone number. Assume a U.S./Canadian number with a 3-digit area code, a 3-digit exchange and another 4 digits. An additional extension is also possible. Assume that the phone number will have dashes or periods in the "usual" way: 718-951-5000 or 718.951.5000. Dashes and periods cannot be mixed. Also, the area code may be surrounded by parentheses: (718)-951-5000. Extensions, if present, will start with an X or an x, preceded by a space or nothing and followed by at least one and no more than four digits. Oh, and one other thing: area codes do NOT start with 0 or 1, do they? What a mess! (But somebody has to do it-- and that's you!) For this exercise you must surround your solution with double quotes.

Explanation / Answer

The regular expression = "(\([2-9]\d{2}\)||[2-9]\d{2})(.\d{3}.\d{4}||-\d{3}-\d{4})(\s[xX]\d{1,4}||[xX]\d{1,4}){0,1}"

Below is the code to test it.

package com.cramster.nov5;

import java.util.Scanner;

public class RegularExpression
{

public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.printf("Enter Phone number : ");
String number = input.nextLine();
String regularExpression =
"(\([2-9]\d{2}\)||[2-9]\d{2})(.\d{3}.\d{4}||-\d{3}-\d{4})" +
"(\s[xX]\d{1,4}||[xX]\d{1,4}){0,1}";
if(number.matches(regularExpression))
System.out.printf("Phone number is in correct format.");
else
System.out.printf("Wrong Phone Number");
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote