0

I'm new to Java programming and we are currently learning about Strings and Chars. My lab assignment is for the user to enter their SSN and for the program to determine whether it is a valid entry. The format entered needs to be 123-45-6789. So far this is what I have done: package Labs;

import java.util.Scanner;

public class Lab4 {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.println("Enter a valid SSN");

        String ssn = input.nextLine();

        System.out.print(ssn);
        if (ssn.length() == 11) {
            if 


        }

    }
}

I've been looking on the forums here and have found some different answers, but when trying them in my code, something goes wrong. I'm not looking for anyone to write it out for me. What type of methods would I use to check that characters 0-2, 4-5, 7-10 contained a number and characters at 3 and 6 contain a -. Obviously charAt and contains would be helpful, but I'm not sure how to write out the conditions to determine this.

And this question asked How to tell if a SSN is in the right format which appears to be the exact same assignment, I assume we are using the same book doesn't actually give an answer, though its marked as a duplicate, which I can not find the original question. We haven't learned arrays yet either, so I am looking for a more simplistic answer, though I'm sure an example using arrays wouldn't hurt in addition to the simpler answer, so that I can develop my skills more.

Edit Update: Though I'm sure there's a better way to do this, it works.

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.println("Enter a valid SSN");

    String ssn = input.nextLine();


    if (ssn.length() == 11 && 
            ssn.charAt(0)>='0' && ssn.charAt(0)<='9' && 
            ssn.charAt(1)>='0' && ssn.charAt(1)<='9' && 
            ssn.charAt(2)>='0'  && ssn.charAt(2)<='9' &&
            ssn.charAt(4)>='0'  && ssn.charAt(4)<='9' &&
            ssn.charAt(5)>='0'  && ssn.charAt(5)<='9' &&
            ssn.charAt(7)>='0'  && ssn.charAt(7)<='9' &&
            ssn.charAt(8)>='0'  && ssn.charAt(8)<='9' &&
            ssn.charAt(9)>='0'  && ssn.charAt(9)<='9' &&
            ssn.charAt(10)>='0'  && ssn.charAt(10)<='9' &&
            ssn.charAt(3)=='-' && ssn.charAt(6)=='-') 
            {
        System.out.println(ssn + " is a valid social security number");
    }
        else
            System.out.print(ssn + " is not a valid social security number");

    input.close();



}

}

Joe
  • 3
  • 5

1 Answers1

0

First of all, I would avoid nested ifs, like that:

if (foo==bar) {
     if(that!=other) {
         if(...)
     }
}

This can quickly lead to chaos. Try to make your if statements parallel, not nested. Like that:

if (ssn.length() != 11) {
    System.out.print("SSN must be 11 letters long");
    return;
}

if (ssn.charAt(3)!='-' || ssn.charAt(7)!='-') {
    System.out.print("SSN must be 11 letters long");
    return;
}

Second, you need to check if a char is a digit. You can do that by a simple nummeric comparison:

if (ssl.charAt(0)<'0' || ssl.charAt(0)>'9') {
    System.out.print("Char at 0 was not a digit");
    return;
}

It would be also good practice to extract the repetitive part (like the is-digit check mentioned above) in a separate method. This way you avoid repeating yourself and you gain much better control over your program.

Hope this helps.

Alkis Mavridis
  • 1,090
  • 12
  • 28
  • Well this certainly does help, but the reason for the nesting was that there are only two possible output statements "123-45-6789 is a valid social security number" or "12-345-6789 is not a valid social security number" How would I check all these conditions and then output the first statement? I kept them nested, but the output statement wouldnt actually show up, so now I am in the process of un-nesting them to see if that works. – Joe Oct 13 '18 at 02:22
  • multiple nesting ifs is the wrong solution to your problem, it is a pattern that should be avoided at all costs, except if there is a VERY good reason to do it. And there is almost never one. In your case, you can definitely avoid it. just define 2 string variables to avoid duplication: final String successMes=" is a valid social security number"; final String failureMes=" is not a valid social security number"; Then, use those strings like System.out.println(ssn+failureMes); return; If all failure-detecting ifs are not executed, and your function reaches the end you print the success message. – Alkis Mavridis Oct 13 '18 at 03:10
  • I ended up using one if and one else statement. The if featured all of the conditions needed and the printed out the first successful message, the else printed out the second failure message. I'll update the original post to show my code. – Joe Oct 13 '18 at 03:49