So I need to write a program to decide if a SSN is valid or not. I have seen this question posted here before but it looks like everyone else is using things a bit more advanced than what I learned so far. I have this so far:
import java.util.Scanner;
public class Homework4_21 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("Enter a SSN: ");
String ssn = input.next();
String number = (String.valueOf('0') +
String.valueOf('1') +
String.valueOf('2') +
String.valueOf('3') +
String.valueOf('4') +
String.valueOf('5') +
String.valueOf('6') +
String.valueOf('7') +
String.valueOf('8') +
String.valueOf('9'));
if ((number.contains(ssn.substring(0, 1))) && (number.contains(ssn.substring(1, 2))) && (number.contains(ssn.substring(2, 3))) && (ssn.substring(3, 4) == String.valueOf('-')) && (number.contains(ssn.substring(4, 5))) && (number.contains(ssn.substring(5, 6))) && (ssn.substring(6, 7) == String.valueOf('-')) && (number.contains(ssn.substring(7, 8))) && (number.contains(ssn.substring(8, 9))) && (number.contains(ssn.substring(9, 10))) && (number.contains(ssn.substring(10, 11))) && (ssn.length() == 11) )
System.out.print(ssn + " is a valid social security number.");
else
System.out.print(ssn + " is an invalid social security number.");
}
}
The idea is that I make a string, 'number', which has all numbers from 0-9 and then compare that to each numerical value in the string 'ssn' to make sure there are no letters or characters in those spaces. I also check to see if the '-' were put in the correct places. Now my problem is whenever I enter a valid SSN I get the invalid response. I tested each part of the if statement to make sure that each substring provides the correct value. For example an input
Enter a SSN: 123-45-6789
Would print
123-45-6789 is an invalid social security number.