Using Java I need to write a program that prompts the user t
Using Java, I need to write a program that prompts the user to enter a Social Security number in the format DDD-DD-DDDD, where D is a digit. Your program should check whether the input is valid. So If I entered the number 232-23-5435 the program would display the output \"232-23-5435 is a valid social security number\". If I entered the number 23-23-5435, the program would display the output \"23-23-5435 is an invalid social security number\". I am enrolled in a Computer Programming I course so the easier this is to understand, the better.
Solution
Check.java
import java.util.Scanner;
public class Check {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println(\"Ener SSN No:\");
String ssn = scan.nextLine();
String status = isValidSSN(ssn);
System.out.println(ssn+\" is \"+status);
}
public static String isValidSSN(String s ){
int length = s.length();
if(length == 11 || length == 9){
if(length == 11) {
if(s.contains(\"-\") || s.contains(\" \")){
String each[] = null;
if(s.contains(\"-\")){
each = s.split(\"-\");
}
else{
each = s.split(\" \");
}
if(each.length == 3){
if(each[0].length() == 3 && each[1].length() == 2 && each[2].length() == 4){
return \"a valid social security number\";
}
else{
return \"an invalid social security number\";
}
}
else{
return \"an invalid social security number\";
}
}
else{
return \"an invalid social security number\";
}
}
else {
int flag = 0;
for(int i=0; i<length; i++){
if(!Character.isDigit(s.charAt(i))){
flag = 1;
break;
}
}
if(flag == 1){
return \"an invalid social security number\";
}
else{
return \"a valid social security number\";
}
}
}
else{
return \"an invalid social security number\";
}
}
}
Output:
Ener SSN No:
232-23-5435
232-23-5435 is a valid social security number
Ener SSN No:
23-23-5435
23-23-5435 is an invalid social security number

