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

Can help solving the following assignment? Language used is Java. Below are the

ID: 3795689 • Letter: C

Question

Can help solving the following assignment?

Language used is Java.

Below are the only information about the assignment. The instructor gave only these information. please help as much as you can.

- Create a program that identifies a string using regular expression matching.

- See the sample run below

.

Chapter 10 bash 70x21 infornansys tens: Chapter 10 SCsis javac What IsIt.java infornansystens Chapter 10 SCS1S java MhatISIt Enter a SSN: 999-99-9999, or a Phone Number: (999) 999-9999 or an ISBN: 999999999X (800) 555-1212 This is a Phone Number. infornansystens :Chapter 10 scsis java MhatISIt Enter a SSN: 999-99-9999, or a Phone Number: (999) 999-9999, or an ISBN: 999999999X 1234567U This is an unknown format. infornansystems:Chapter 10 SCS1$ I

Explanation / Answer

For SSN:

Code:

import java.util.*;
import java.util.regex.*;
class Validations {
public static void main(String args[]) {
   Scanner keyboard = new Scanner(System.in);
   System.out.println("Enter SSN:999-99-9999:");
   String SSN=keyboard.nextLine();
   String regex = "^(?!000|666)[0-8][0-9]{2}-(?!00)[0-9]{2}-(?!0000)[0-9]{4}$";
Pattern pattern = Pattern.compile(regex);
   Matcher matcher = pattern.matcher(SSN);
   if(matcher.matches())
   System.out.println("Valid SSN");
   else
   System.out.println("Invalid SSN");
   }
   }

Similarly

for Phone number:

Refular expression is:^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$";

for ISBN:

^[0-9X]{10}$";

-------------------------------------------------------------------------------------------------------------------------------------------

import java.util.regex.*;
class Validations {
public static void main(String args[]) {
   Scanner keyboard = new Scanner(System.in);
   System.out.println("Enter SSN:999-99-9999:, or Enter Phone number :(999) 999-9999,or an ISBN:99999999X: ");
   String input=keyboard.nextLine();
   String regexSSN = "^(?!000|666)[0-8][0-9]{2}-(?!00)[0-9]{2}-(?!0000)[0-9]{4}$";
   String regexPHNE ="^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$";
   String regexISBN="^[0-9X]{10}$";
   Pattern patternSSN = Pattern.compile(regexSSN);
   Pattern patternPHNE = Pattern.compile(regexPHNE);
   Pattern patternISBN = Pattern.compile(regexISBN);
   Matcher matcherssn = patternSSN.matcher(input);
   Matcher matcherphne = patternPHNE.matcher(input);
   Matcher matcherISBN = patternISBN.matcher(input);
       if(matcherssn.matches()){
   System.out.println("Valid SSN");
}else if(matcherphne.matches()){
   System.out.println("Valid Phone Number");
}
else if(matcherISBN.matches()){
   System.out.println("Valid ISBN");
}
else{
System.out.println("Invalid format");
}
   }
   }

--------------------------------------------------------------------------

sample output:

123-45-6789

Valid SSN