Question
Imy trying to make an SSN checker, where I have to validate the formatting of a user input ssn. I cannot use string regex to
Imy trying to make an SSN checker, where I have to validate the formatting of a user input ssn. I cannot use string regex to do this, and must use the split method instead. before, I tried to make it split the string using the gaps between letters as a delimeter, instead ive moved the split method further into my nested statements.
What ive been trying to acheive is to make it check all these conditions and when it fails one to throw my custom exception. but Im worried about the way my conditionals are set up. I keep getting else without if errors and no matter how I move the brackets around i break it somehow.
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{ // Cleaning up the input ssn[i].trim(); // check to see if the ssn is formatted properly if(ssn.charAt(3) == !'-' && ssn.charAt(6) == !'-'){ throw new InvalidSSNException(); } // check ro aee if the ssn is exactly 11 characters else if(ssn.length() == !11){ throw new InvalidSSNException(); String[]usrssn = ssn.split("-"); } for(int i = 0; i < usrssn.length(); i++){ // check that the characters at these indexes are digits else if(Character.isLetter(i)){ throw new InvalidSSNException(); } else{ return true; System.out.println( ssn + " " +"This SSN meets all the criteria!"); } } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started