2

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.

xxx-xx-xxxx

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 if-else statements but its getting pretty tedious.

Example:

if (x > 0 && x<3)
  return "New Hampshire";
else if (x <= 7)
  return "Maine";
 ...
dbc
  • 104,963
  • 20
  • 228
  • 340
Daniel Cole
  • 49
  • 1
  • 8
  • 2
    Just so you're aware, you can't tell where they were born, just where they resided when it was issued. I was born in one state, but didn't get an SSN issues until years later when I live in another state. I found my prefix on that chart you linked to, and it clearly shows where I lived when my parents got me an SSN, which was years later and halfway across the country from when I was born. Lesson: Be sure you understand the actual business rules/process in place before you try coding around it. ;-) – David Feb 20 '15 at 02:42
  • 6
    @jsve Homework isn't strictly off-topic on SO, http://meta.stackexchange.com/questions/10811/how-do-i-ask-and-answer-homework-questions – Preston Guillot Feb 20 '15 at 02:43
  • That said, I'd dump the data into a database and use a select statement rather than a bunch of hard-coded IF statements. – David Feb 20 '15 at 02:44
  • @DavidStratton thanks! I'll look into that more because i've ever worked with a database, but it's always worthwhile to go out of your comfort zone and learn something useful – Daniel Cole Feb 20 '15 at 02:54
  • @PrestonGuillot thank you for making me aware of that. I will keep that in mind as I navigate SO. – Sumner Evans Feb 20 '15 at 03:25

3 Answers3

4

You have a few options here:

  1. 50 if statements, one for each state, as you are doing.
  2. A 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.
  3. 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. :)

lc.
  • 113,939
  • 20
  • 158
  • 187
  • Thanks man! I eventually went with something along the likes of method 3. I'm just getting used to using Microsoft Visual Studio so I added the file as a resource and then it took me a little while to get all the bugs out of my code, but eventually I got it to work perfectly! – Daniel Cole Feb 20 '15 at 06:28
1

It's hard to tell your level of skill and what your course has taught you so far which is why it's difficult answering these kinds of questions, and also for the most part that's why you will get a negative response from people - they assume that you would have had the answer in your course materials already and will assume that you are being lazy.

I'm going to assume that you are at a basic level and that you already know how to solve the problem the brute force way (your if/else construct) and that you are genuinely interested in how to make your code better and not simply asking for a solution you can copy/paste.

Now, while your if/else idea will work, that is a procedural way of thinking. You are working with an object oriented language, so I would suggest to you to think about how you could use the principles of OO to make this work better. A good starting point would be to make a collection of state objects that contain all the parameters you need. You could then loop through your state collection and use their properties to find the matching one. You could create the state collection by reading from a file or database or even just hard coding it for the purposes of your assignment.

Darko
  • 38,310
  • 15
  • 80
  • 107
  • 1
    Thanks for understanding, this part of the assignment was optional and was solely for learning more about coding (its not graded) so I genuinely was curious as to how to be a better, more efficient coder. – Daniel Cole Feb 20 '15 at 06:31
0

Your intuition that a long chain of if and else if statements might be unwieldy is sound. Not only is it tedious, but the search to find the correct interval is inefficient. However, something needs to be in charge of this tedium. A simple solution would be to use a Dictionary to store key/value pairs that you could build up once, and reuse throughout the application. This has the downside of requiring more space than necessary, as every individual mapping becomes an element of the data structure. You could instead follow the advice from this question and use a data structure more suited to ranged values for look ups.

It's difficult to tell from your question as it's written what your level of expertise is, and going into any real detail here would essentially mean completing your assignment for you. It should be noted though, that there's nothing inherently wrong with your solution, and depending on where you are in your education it may be what's expected.

Community
  • 1
  • 1
Preston Guillot
  • 6,493
  • 3
  • 30
  • 40