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

Ive written a program that is meant to check a user input ssn and give error mes

ID: 3740381 • Letter: I

Question

Ive written a program that is meant to check a user input ssn and give error messages when they dont display it properly. I created a custom exception as that is what the assignment called for, however Im having difficulties getting it to complie. I had thought that one I cleared out the last of the errors that I would be good, however 20 more popped up after deleting an extra curly brace.

Here is my custom exception class:

public class InvalidSSNException extends Exception {
public InvalidSSNException() {
super("Reason: Social Securtiy numbers" +
"must be in the format of DDD-DD-DDDD, where D is a digit.");
}
}

Here is my application:

import java.util.Scanner;

public class SSNChecker{


public static void main (String[] args) {
  
Scanner scan = new Scanner(System.in);  
System.out.println("Enter your Social Security number: ");
scan.close();

  
try{
String ssn = scan.nextLine();
ssn.isValidSocial();

}
catch(InvalidSSNException e){
systerm.out.println(e);
}

}
  
public boolean isValidSocial(String ssn){
  
String[]ssn = usrssn.split("");
// Cleaning up the input
String usrssn = ssn[i].trim();
// checking to see if user input is exactly 11 characters long.  
if(usrssn.length() = 11){
return true;
  
for(int i = 0; i < ssn.length(); i++){
// checking to see if the social is formated correctly.
if(usrssn.charAt(3)= '-' && usrssn.charAt(6)= '-'){
return true;
//checking to make sure the characters are digits by index.
if(Charater.isDigit(0) && Charater.isDigit(1) &&
Charater.isDigit(2) && Charater.isDigit(4) &&
Charater.isDigit(5) && Charater.isDigit(7) &&
Charater.isDigit(8) && Charater.isDigit(9) &&
Charater.isDigit(10)){
  
return true;
System.out.println( ssn + "" +"This SSN meets all the criteria!");

}
}
else{
throw InvalidSSNException;
return false;

}
}
}
}
}
}
  

Explanation / Answer

Answer:

SSNChecker.java:

import java.util.Scanner;

public class SSNChecker{

public static void main (String[] args) {

Scanner scan = new Scanner(System.in);  

System.out.println("Enter your Social Security number: ");

try{

String ssn = scan.nextLine();

scan.close();

isValidSocial(ssn);

}

catch(InvalidSSNException e){

System.out.println(e);

}

}

public static boolean isValidSocial(String ssn) throws InvalidSSNException{

if(ssn.length() == 11){

String regex = "[0-9]+"; // regular expression to check the sub string is containing only digits

if(ssn.charAt(3) == '-' && ssn.charAt(6) == '-'){// check the position 4 and 7 are - or not

//checking to make sure the characters are digits by index.

if(ssn.substring(0, 2).matches(regex) &&

ssn.substring(4, 5).matches(regex) &&

ssn.substring(7, 10).matches(regex)){

System.out.println( ssn + " " +"This SSN meets all the criteria!");

return true;

}

else{

throw new InvalidSSNException();

}

}

else{

throw new InvalidSSNException();

}

}

else{

throw new InvalidSSNException();

}

}

}

InvalidSSNException.java:

public class InvalidSSNException extends Exception {

public InvalidSSNException() {

super("Invalid SSN"+

" Reason: Social Securtiy numbers" +

"must be in the format of DDD-DD-DDDD, where D is a digit.");

  

}

}

Output_1:

Enter your Social Security number:
321-54-9867
321-54-9867 This SSN meets all the criteria!

Output_2:

Enter your Social Security number:
12345678910
InvalidSSNException: Invalid SSN
Reason: Social Securtiy numbersmust be in the format of DDD-DD-DDDD, where D is a digit.