import java.util.*;
public class SSN {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println(" Enter a valid social security number in a valid style DDD-DD-DDDD");
String SSN = input.nextLine(); // String SSN
// SSN.compareTo("123-45-6789");
// SSN = "123-45-6789";
String social = "123-45-6789";
if (isValid(SSN)) {
System.out.println("Valid Social Security Number");
}
else
System.out.println("Invalid Social Security Number");
input.close();
}
private static boolean isValid(String SSN) {
if(! SSN.matches("\\d{3} - \\ d{2} - \\d{4}")); //"123-45-6789"
return false;
boolean social = true;
for(int i = 0; i < SSN.length(); i++){
switch(SSN.charAt(i)){
case 1:
if(! Character.isDigit(SSN.charAt(i))) social = false;
break;
case 2 :
if(!('-' == SSN.charAt(i))) social = false;
break;
default :
break;
}
}
return social;
}
}
This post has been edited by modi123_1: 25 February 2014 - 08:25 PM
Reason for edit:: highlight your text and click the 'code' button

New Topic/Question
Reply


MultiQuote


|