aspose file tools*
The moose likes Beginning Java and the fly likes Checking SSN without regular expressions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Checking SSN without regular expressions" Watch "Checking SSN without regular expressions" New topic
Author

Checking SSN without regular expressions

Leesa Gabor
Greenhorn

Joined: Jun 05, 2012
Posts: 13
Prompt user to enter a social security number in the format DDD-DD-DDDD, where D is a digit. Displays "Valid SSN" for a correct ssn, and "Invalid SSN" otherwise.

I am looking for ideas on alternate ways to accomplish this problem. Please no regular expressions. I have it working I am just looking for other ways to solve this with an array maybe or something simpler. I have used if statements here:

Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

Hi Leesa,

Welcome to the Ranch!

It's a tradition here that we have to give greenhorns like you some good-natured ribbing about your first question, just to make sure you'll fit in. We have a black and white chicken out back. We'd like to know how many black feathers and how many white feathers it has. Looks like you have just the kind of patience needed for the job, the way you checked that SSN.

Just kidding. And it's not really a tradition (In fact, I might even get in trouble with the bosses for giving the greenhorn a hard time)

One thing that comes to mind is the java.util.Scanner (click on the class name to go to the JavaDocs) You can specify the delimiter as a "-" and get 3 ints from it. Then you can check the values if they each fall within the appropriate ranges. It's kind of a cheat because Scanner most likely uses a RegEx internally but you'll never know because the class hides those details from you. Don't take my word for it though. Gotta go feed that chicken now...


Junilu - [How to Ask Questions] [How to Answer Questions]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

BTW, Leesa, if you're posting code snippets, click on the "Code" button in the editor and put your code snippets between the code "tags" that get created. That will make your code display nicely when you post your message, assuming you format it nicely in the editor.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18843
    
  40

Leesa Gabor wrote:
I am looking for ideas on alternate ways to accomplish this problem. Please no regular expressions. I have it working I am just looking for other ways to solve this with an array maybe or something simpler. I have used if statements here:




Before you look for other ways to solve this, it may be a good idea to make sure that the current version actually works.

Question: What happens when you check "AAA-BB-CCCC" to see if it is a valid SSN?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1021
    
    5

Further to Henry's post, what happens when the value 123-45 is passed? I think you might run into a problem when assigning a character value to index7.
Leesa Gabor
Greenhorn

Joined: Jun 05, 2012
Posts: 13
@James and Henry: Thanks, I think I fixed the validity problem by combining the if statements to first check if the length is right and then continue to check the characters.

@ Junilu, Thanks for the tips haha, as you can tell I am new to this stuff. It looks like the scanner does use regular expressions. But that is a good idea and I will consider figuring out how to make it work like that.

So are there any other problems you see or ideas for me to use an array?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
A Scanner uses a regular expression for the delimiter. It does not use a regular expression to match the whole String. You can write regular expressions to match the whole of an SSN, and I would suggest you use the Java tutorials as an introduction to regexes (?regices?).
Leesa Gabor
Greenhorn

Joined: Jun 05, 2012
Posts: 13
I know I could take the time to learn regular expressions or scanner to do this, but as far as I have learned in my class we have not studied them. We have however used arrays, so I was trying to find a way to apply arrays to this problem or something else we have learned. So far we'eve covered Data Types, Variables, Arithmetic, Selection, Repetition, Methods, Array, Classes and Objects, and now we are working with Strings. This is a very beginner level Java class. I figured it out with If statements but in class people always come up with more simple solutions which require less code and I am having a hard time thinking of other ways to solve the problem.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
For a start, there is a simpler way to get a char array from a String. Look through the String class and you should find something useful there.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18843
    
  40

Leesa Gabor wrote:@James and Henry: Thanks, I think I fixed the validity problem by combining the if statements to first check if the length is right and then continue to check the characters.

@ Junilu, Thanks for the tips haha, as you can tell I am new to this stuff. It looks like the scanner does use regular expressions. But that is a good idea and I will consider figuring out how to make it work like that.

So are there any other problems you see or ideas for me to use an array?



Same question: What happens when you check "AAA-BB-CCCC" to see if it is a valid SSN?

Interestingly, while this version no longer throws an exception, it actually is less reliable -- it fails more tests than the previous version. Heck, with this new version, even "ABCDEFGHIJK" will pass as an SSN.

Henry
Leesa Gabor
Greenhorn

Joined: Jun 05, 2012
Posts: 13
So I thought isDigit was supposed to check to see if the character is a number. Either digit does not mean number, or somehow my code overrides the false returned when it is not a number.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

Leesa, I see you're going to fit right in here, despite your soft little un-calloused Java hands

See, the problem with using an array and the method you just tried is that yeah you can do it but it's like trying to bag that big pile of corn grits over yonder (pointing finger randomly over your shoulder) with a teaspoon. Yeah, we could give you some suggestion on how you might do it even though we'd be snickering under the brims of our hats. But that's just plain being mean and we're not like that in these here parts, Li'l lady. I really see no point in giving you a teaspoon when a shovel will do you much better and faster.

Anyway, enough of the ranch talk. It's getting tedious for me, being Asian and more accustomed to wading around in the rice paddies (that's not racist, BTW, because I actually did that growing up). The reason I don't like the idea of using an Array is that a String is actually already just as good as an array. It even has a toCharArray() method. So why bother moving the grits from one set of bucket to another set of buckets? That's just additional work, right? Just find me something that makes it easy for me to get these grits out of the daggum buckit! (Cue left, java.util.Scanner).

If you really want to sully your smock with some buckets, er, arrays, ask one of these handsome (or beautiful, as the case may be -- we're equal opportunity around here) bartenders for a more appropriate challenge.
Leesa Gabor
Greenhorn

Joined: Jun 05, 2012
Posts: 13
Ah, I think I see what you are saying. Since you can access the characters in a string using the index, it is basically already an array of characters. So then are you saying it would be pointless to put the characters in an array since I can already acces them in pretty much the same way as I would if they were in an array?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18843
    
  40

Leesa Gabor wrote:I know I could take the time to learn regular expressions or scanner to do this, but as far as I have learned in my class we have not studied them. We have however used arrays, so I was trying to find a way to apply arrays to this problem or something else we have learned. So far we'eve covered Data Types, Variables, Arithmetic, Selection, Repetition, Methods, Array, Classes and Objects, and now we are working with Strings. This is a very beginner level Java class. I figured it out with If statements but in class people always come up with more simple solutions which require less code and I am having a hard time thinking of other ways to solve the problem.



BTW ... Just in case you might be interested to see how a regular expression solution would look like, here is your method ported to use regular expressions...



Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18843
    
  40

Leesa Gabor wrote:So I thought isDigit was supposed to check to see if the character is a number. Either digit does not mean number, or somehow my code overrides the false returned when it is not a number.


Well, it is generally not very productive to assume that the library is faulty as the first speculation .... but regardless, if you suspect that it is so, then you should write a test application to try it out. Test it to see if the isDigit() method works.

Henry
Leesa Gabor
Greenhorn

Joined: Jun 05, 2012
Posts: 13
That looks so simple, I can see why using regular expressions would be the first choice of you knowledgeable people So I have a test already and I know it does not work, I just was not pasting the test part into here. But here is all of it:
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
Leesa Gabor wrote:Ah, I think I see what you are saying. Since you can access the characters in a string using the index, it is basically already an array of characters. So then are you saying it would be pointless to put the characters in an array since I can already acces them in pretty much the same way as I would if they were in an array?
No, that is not what we are saying at all. A String is not an array of characters; it even says so in the Java Language Specification. But it is very easy to get an array of chars out of it, and Junilu Lacar has already told youhow it is done.
Yes, Character.isDigit() is returning the correct thing.
I think you need to get a pencil and paper and go through the execution of your latest attempt on paper. Write down the value of your valid field if you pass 123-45-6789 or ABC-DE-FGHI or 123456. Follow where the execution goes to, and see what is happening.
You will also find, if you look here, that if (...) return true; else return false; is regarded as poor style. There is a shorter style which you ought to use.
Leesa Gabor
Greenhorn

Joined: Jun 05, 2012
Posts: 13
Okay, I got rid of that last if statement. It seems to be working now, with the AAA-BB-CCCC being Invalid. Perhaps I should combine the length check into the assignment to valid as well.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

If you must insist on doing it this way, then fine.

Line 30: assigning true to valid here is pointless. The value assigned here is going to be clobbered on line 48 anyway.

You could get rid of a lot of clutter by refactoring (improving the design of the code without changing its functionality) -- improving design includes improving its readability.

I would just extract 'Character.isDigit(...)' and get rid of all the indexN variables. (That is, if I were doing it this way in the first place)



Man, I think I got carpal tunnel syndrome just typing that!
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

Also, a common convention for naming methods that return a boolean value is make it start with "is" or "has" or "can" or something that indicates it's going to be a Yes/No, true/false thing.

Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

Better yet, use variable argument list for isDigit:


I give up As they say around here, "It's like putting lipstick on a pig."

[EDIT] Junilu, I broke up that long final line; it screws up the windowing.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
That’s so much better. Even if you aren’t creating objects to call the isDigit method on. Wouldn’t areDigits be a better name? Of course, you can condense Junilu’s code toIf you search, you can find some idiot’s classification of methods. You find those methods require information from the outside world and return information, but neither require nor alter any information from an object. So they are good candidates for being static.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

"letting this code loose on an unsuspecting world" ... I like that

A noble effort, Campbell, but that's still about the prettiest pig you'll ever get.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
It’s your code, enhanced slightly. And I am sure a pretty pig will make tasty bacon
Bill Clar
Ranch Hand

Joined: Sep 21, 2006
Posts: 152

Pardon my greenhorn naivete, but I wanted to take a crack as this one.

I used the Integer class to determine if the input is numeric.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
Bill Clar wrote: . . .
Not bad. Only a few points I would pick up, as mentioned above. I still prefer the Junilu Lacar approach, however.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
You can reduce the first few lines to boolean valid = ssn.length() == 11;
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

Well, I got a bigger bone to pick with Bill: Look at this http://stackoverflow.com/questions/465953/throwing-exceptions-to-control-flow-code-smell and see if you can see any similarities to your approach. That, young'un, is called a big code smell. Round these parts, we try to set folks back on the right path but if you insist on doing this again I'm going to have to make you clean that up and march you right out of town.

And didn't I already tell the young lady before that "checkSsn()" ain't as clear as "isValid(ssn)"? Or were you not payin' attention?

Here's a nice summary of rules about exceptions: http://nicodewet.com/2011/10/02/java-exception-rule-book/
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7801
    
  21

Leesa Gabor wrote:Prompt user to enter a social security number in the format DDD-DD-DDDD, where D is a digit. Displays "Valid SSN" for a correct ssn, and "Invalid SSN" otherwise.

Well the first thing I would do is to create an SSN class. Then you can encapsulate all the rules for entering and validating it (whatever they are and however you decide to do it) inside the class.

Personally, the format of the code suggests to me that an SSN is actually made up of three numeric parts; and numbers are usually best stored as numbers, not Strings - however, if anyone knows better, please feel free to debunk.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2377
    
  28

Leesa Gabor wrote:
So are there any other problems you see or ideas for me to use an array?


You could generate all possible strings of the format NNN-NN-NNNN and store it in a huge array of strings. Then you can iterate over your array of strings to check if your input string matches any of those strings

Hey you said, you wanted to do something differrent. You didn't say it has to be good :p
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18843
    
  40

Leesa Gabor wrote:Okay, I got rid of that last if statement. It seems to be working now, with the AAA-BB-CCCC being Invalid. Perhaps I should combine the length check into the assignment to valid as well.



Technically, you can combine everything down to a single expression...



Although it is debatable on just how readable it is.

Henry
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1021
    
    5



A little bit of efficiency never hurt anyone...
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

James Boswell wrote:

A little bit of efficiency never hurt anyone...


Others much smarter than me might disagree: "Premature optimization is the root of all evil." -- Donald Knuth
Bill Clar
Ranch Hand

Joined: Sep 21, 2006
Posts: 152

Junilu Lacar wrote:Well, I got a bigger bone to pick with Bill: Look at this http://stackoverflow.com/questions/465953/throwing-exceptions-to-control-flow-code-smell and see if you can see any similarities to your approach. That, young'un, is called a big code smell. Round these parts, we try to set folks back on the right path but if you insist on doing this again I'm going to have to make you clean that up and march you right out of town.

And didn't I already tell the young lady before that "checkSsn()" ain't as clear as "isValid(ssn)"? Or were you not payin' attention?

Here's a nice summary of rules about exceptions: http://nicodewet.com/2011/10/02/java-exception-rule-book/


Junilu, the links you provided proved insightful. Thanks!
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

Bill Clar wrote:Junilu, the links you provided proved insightful. Thanks!


Bill, you just brought a warm ray of sunshine into my morning. You don't know how much better that makes me feel today, sincerely.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7801
    
  21

Leesa Gabor wrote:I am looking for ideas on alternate ways to accomplish this problem. Please no regular expressions. I have it working I am just looking for other ways to solve this with an array maybe or something simpler.

Going back to the original question: the fact is that, until something better comes along, regexes are probably the way to deal with this; so unless this is simply a mental exercise, I would use them.
1. They were designed to solve exactly this sort of problem.
2. All the alternative solutions that have been proposed are either (a) inferior, or (b) less generic.
3. They've been worked on by countless geeks and experts for 30 years or more, so the code is pretty mature. Furthermore, its performance is only likely to improve.

Part of programming is choosing the best tool for the job - and in this case, it's a regex.

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Checking SSN without regular expressions