Imy trying to make an SSN checker, where I have to validate the formatting of a
ID: 3740479 • Letter: I
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 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!");
}
}
}
}
Explanation / Answer
import java.util.Scanner;
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.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 not exactly 11 characters
else if(ssn.length() != 11){
throw new InvalidSSNException();
//String[] usrssn = ssn.split("-");
}
// check ro aee if the ssn is exactly 11 characters
String[] usrssn = ssn.split("-");
// to get the length of array use usrssn.length instead of usrssn.length()
for(int i = 0; i < usrssn.length /* () */; i++)
{
// check that the characters at these indexes are digits
if(Character.isLetter(i))
{
throw new InvalidSSNException();
}
else
{
System.out.println( ssn + " " +"This SSN meets all the criteria!");
return true;
}
}
// a return type is required here
return true;
}
}
// replace this class with your own class
class InvalidSSNException extends Exception
{
}