-
-
Save bytecodeman/ad91fd1c09d546ede66deed77f4be1d9 to your computer and use it in GitHub Desktop.
CSC-111 Two SSN Verifier Solutions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Prof. Antonio C Silvestri | |
* 10/18/2016 | |
* CSC-111 D01 | |
* Social Security # Verifier | |
* silvestri@stcc.edu | |
*/ | |
import java.util.Scanner; | |
public class SSNVerifier { | |
public static void main(String[] args) { | |
Scanner input = new Scanner(System.in); | |
System.out.print("Please enter an SSN to test for validity: "); | |
String socialSecNum = input.nextLine(); | |
socialSecNum = socialSecNum.trim(); | |
if (socialSecNum.length() == 9) { | |
boolean isValid = true; | |
isValid = isValid && Character.isDigit(socialSecNum.charAt(0)); | |
isValid = isValid && Character.isDigit(socialSecNum.charAt(1)); | |
isValid = isValid && Character.isDigit(socialSecNum.charAt(2)); | |
isValid = isValid && Character.isDigit(socialSecNum.charAt(3)); | |
isValid = isValid && Character.isDigit(socialSecNum.charAt(4)); | |
isValid = isValid && Character.isDigit(socialSecNum.charAt(5)); | |
isValid = isValid && Character.isDigit(socialSecNum.charAt(6)); | |
isValid = isValid && Character.isDigit(socialSecNum.charAt(7)); | |
isValid = isValid && Character.isDigit(socialSecNum.charAt(8)); | |
System.out.println(socialSecNum + " is " + ((isValid) ? "a valid" : "an invalid") + " SSN."); | |
} | |
else if (socialSecNum.length() == 11) { | |
boolean isValid = true; | |
isValid = isValid && Character.isDigit(socialSecNum.charAt(0)); | |
isValid = isValid && Character.isDigit(socialSecNum.charAt(1)); | |
isValid = isValid && Character.isDigit(socialSecNum.charAt(2)); | |
isValid = isValid && socialSecNum.charAt(3) == '-'; | |
isValid = isValid && Character.isDigit(socialSecNum.charAt(4)); | |
isValid = isValid && Character.isDigit(socialSecNum.charAt(5)); | |
isValid = isValid && socialSecNum.charAt(6) == '-'; | |
isValid = isValid && Character.isDigit(socialSecNum.charAt(7)); | |
isValid = isValid && Character.isDigit(socialSecNum.charAt(8)); | |
isValid = isValid && Character.isDigit(socialSecNum.charAt(9)); | |
isValid = isValid && Character.isDigit(socialSecNum.charAt(10)); | |
System.out.println(socialSecNum + " is " + ((isValid) ? "a valid" : "an invalid") + " SSN."); | |
} | |
else | |
System.out.println(socialSecNum + " is an invalid SSN."); | |
input.close(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Prof. Antonio C Silvestri | |
* 10/18/2016 | |
* CSC-111 D01 | |
* Social Security # Verifier | |
* silvestri@stcc.edu | |
*/ | |
import java.util.Scanner; | |
public class SSNVerifier2 { | |
public static void main(String[] args) { | |
Scanner input = new Scanner(System.in); | |
System.out.print("Please enter an SSN to test for validity: "); | |
String socialSecNum = input.nextLine(); | |
socialSecNum = socialSecNum.trim(); | |
if (socialSecNum.length() == 9) { | |
boolean isValid = true; | |
for (int i = 0; isValid && i < socialSecNum.length(); i++) | |
isValid = isValid && Character.isDigit(socialSecNum.charAt(i)); | |
System.out.println(socialSecNum + " is " + ((isValid) ? "a valid" : "an invalid") + " SSN."); | |
} else if (socialSecNum.length() == 11) { | |
boolean isValid = true; | |
for (int i = 0; isValid && i < socialSecNum.length(); i++) | |
if (i == 3 || i == 6) | |
isValid = isValid && socialSecNum.charAt(i) == '-'; | |
else | |
isValid = isValid && Character.isDigit(socialSecNum.charAt(i)); | |
System.out.println(socialSecNum + " is " + ((isValid) ? "a valid" : "an invalid") + " SSN."); | |
} else | |
System.out.println(socialSecNum + " is an invalid SSN."); | |
input.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment