DEV Community

Edwin Torres
Edwin Torres

Posted on • Edited on

5

Algorithm Development: Verifying a Social Security Number

A person's social security number is in a specific format with numbers and dashes:

nnn-nn-nnnn

For example, this is an example of a valid social security number:

111-22-3333

Note that this number is fictitious for this article.

To verify that a social security number is valid, follow these rules:

  • The social security number must be a string.
  • The string must be of length 11.
  • Starting with position 0, there are hyphen (-) characters in the 3rd and 6th positions.
  • All other characters in the string must be digits in the range 0 to 9.

Here are some sample social security numbers that fail to meet these requirements:

  • 111223333 - length is not 11 and hyphens are missing
  • 11-1223-333 - hyphens are in the wrong positions
  • 111-aa-3333 - non-numeric characters are in positions 4 and 5
  • 111-22-333 - length is not 11

Here is one possible algorithm to verify the format of a social security number:

  1. Input the social security number as a string value.
  2. Check the length of the social security number.
    • If the length is not equal to 11, output a message that the social security number is invalid and exit the program.
    • Otherwise, continue to the next step.
  3. Check that positions 3 and 6 of the social security number are hyphen (-) characters. One way to do this is with the Java String charAt() method.
    • If one or both of the positions is not the hyphen (-) character, output a message that the social security number is invalid and exit the program.
    • Otherwise, continue to the next step.
  4. Check that all other positions in the string are numbers in the range 0 - 9. One way to do this is with the Java String method isDigit().
    • If at least one of these characters is not a digit, output a message that the social security number is invalid and exit the program.
    • Otherwise, output a message that the social security number is valid.

This just one possible algorithm for verifying the format of a social security number. There are other ways to do this. But algorithm development is the focus here. Develop a solid algorithm before writing code. Then it is just a matter of translating the algorithm into real Java statements.

Thanks for reading. 😃

Follow me on Twitter @realEdwinTorres for more programming tips and help.

Top comments (0)

Great read:

Is it Time to go Back to the Monolith?

History repeats itself. Everything old is new again and I’ve been around long enough to see ideas discarded, rediscovered and return triumphantly to overtake the fad. In recent years SQL has made a tremendous comeback from the dead. We love relational databases all over again. I think the Monolith will have its space odyssey moment again. Microservices and serverless are trends pushed by the cloud vendors, designed to sell us more cloud computing resources.

Microservices make very little sense financially for most use cases. Yes, they can ramp down. But when they scale up, they pay the costs in dividends. The increased observability costs alone line the pockets of the “big cloud” vendors.