I am currently working on an assignment in which I am to validate various formats using regular expressions (phone numbers, birth date, email address, Social Security). One of the features our teacher has suggested would be to have a method that returns the state an individual was born using their Social Security Number.
The first 3 digits correspond to a state/area as outlined here: http://socialsecuritynumerology.com/prefixes.php
If I've isolated the first 3 numbers as an integer already, is there anyway I could quickly match the number with its corresponding area code?
Currently I'm only using
else statements but its getting pretty tedious.
if (x > 0 && x<3)
return "New Hampshire";
else if (x <= 7)
Best How To :
You have a few options here:
if statements, one for each state, as you are doing.
switch with 999 conditions, matching each option with a state. It probably looks cleaner and you can generate it with a script and interject the
return statements wherever necessary. Maybe worse than option 1 in terms of tediousness.
Import the file as text, parse it into a
Dictionary and do a simple lookup. The mapping is most likely not going to change in the near future, so the robustness argument is rather moot, but it is probably "simpler" in terms of amount of effort*. And it's another chance to practice regex to parse lines in the file you linked.
*Where "effort" is measured purely in the amount of tedious gruntwork prone to annoying human error and hand fatigue. Energy consumed within the brain due to engineering and implementing a solution where the computer does the ugly stuff for you is not included. :)