2

I have an assignment asking the user to enter a SSN and the program tells whether or not it's valid. I got the base of the program working, but at this point, the user can enter numbers as well as letters and I'm not sure how to fix that. I know about parseInt, but I couldn't figure out how to work it since the input has dashes in it. My professor also told us we can't use loops since there's no need to.

import java.util.Scanner;

public class Exercise04_21 {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    // DDD-DD-DDDD
    System.out.print("Enter a SSN: ");
    String ssn = input.next();


    if (ssn.charAt(3) == '-' && ssn.charAt(6) == '-') {
        if (ssn.length() == 11) {
            System.out.printf("%s is a valid social security number", ssn);
        } else {
            System.out.printf("%s is an invalid social security number", ssn);
        }
    } else {
        System.out.printf("%s is not a valid social security number", ssn);
    }
}

}

5
  • 2
    How about a regular expression instead of these if-tests? Is that allowed? Commented Mar 2, 2020 at 17:03
  • 2
    Integer.parseInt(ssn.replaceAll("-", "")); Commented Mar 2, 2020 at 17:07
  • 1
    I hate those kinds of homework questions. "We can't use X because teacher said so" implies that your teach expects you to solve this question with a set of techniques that a.) we don't know the extent of and b.) almost certainly doesn't include some of the techniques that would be used to solve this "in the real world". This is bound to generate content in the form of "use A" - "no, they told us not to use A" - "then use B" - ... Sorry for the rant. Commented Mar 2, 2020 at 17:21
  • @JoachimSauer Technically we haven't gone over loops, so I understand from that perspective but it still bugs me. Commented Mar 2, 2020 at 17:31
  • @jbrulmans They should be allowed. She really only told us no loops. Commented Mar 2, 2020 at 17:36

2 Answers 2

1

You could try to count the number of dashes, to assert that there are two. Then, try parsing the SSN input with dashes removed as an integer. Should that parse operation not throw an exception, then the input is valid.

String ssn = input.next();
int numDashes = ssn.length() - ssn.replace("-", "").length();
boolean canParse = true;

try {
    int ssnInt = Integer.parseInt(ssn.replace("-", ""));
}
catch (NumberFormatException nfe) {
    canParse = false;
}

if (numDashes == 2 && canParse) {
    System.out.printf("%s is a valid social security number", ssn);
}
else {
    System.out.printf("%s is an invalid social security number", ssn);
}

Of course, you could also make life easy by just using a regular expression:

if (ssn.matches("\\d{3}-\\d{2}-\\d{4}")) {
    // VALID
}

But, perhaps your assignment does not allow for using regular expressions.

Sign up to request clarification or add additional context in comments.

1 Comment

I think regular expressions should be allowed. She really only said no loops since we haven't gone over them yet. Are the d{3}, d{2} and d{4} values I have to express or are those built into the method?
1

You could use a regular expression for something like this. For example:

String regex = "^(?!000|666)[0-8][0-9]{2}-(?!00)[0-9]{2}-(?!0000)[0-9]{4}$";
Pattern pattern = Pattern.compile(regex);
boolean matches = Pattern.matches(pattern, text);

1 Comment

Regex's are super useful in cases like this. You can read up on it, you will def come across this in the future somewhere. More information: docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.