0

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.

EthanBar
  • 667
  • 1
  • 9
  • 24
  • I have no idea why you are creating `number` in that way. I am sure that there are better regex examples to test for valid ssn – Scary Wombat Oct 23 '17 at 02:48
  • Also this `ssn.substring(3, 4) == String.valueOf('-')` is not how you compare Strings in java – Scary Wombat Oct 23 '17 at 02:49
  • In real life with things like ssns or phone numbers, it’s better to store them as unformstted. Validating that the right formatting chars are present seems pointless b – Nathan Hughes Oct 23 '17 at 02:59
  • Yes @NathanHughes but OP's assignment was to validate a formatted SSN, so the point is moot. – Kevin Anderson Oct 23 '17 at 03:28
  • I believe 123-45-6789 is indeed an invalid SSN. It has never been issued and will probably never be. Neither will any SSN that contains all zero's in **any** digit group (Area, Group, or Serial). Neither will any SSN that starts with 666. There will also (most likely) never be any SNN's where the Area (first three numbers) is greater than 773 (or is it 799 - can't remember). In any case locate some validity rules for the SSN. – DevilsHnd - 退職した Oct 23 '17 at 05:00

0 Answers0