Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Help with String class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with String class" Watch "Help with String class" New topic
Author

Help with String class

Mel Ram
Greenhorn

Joined: Sep 30, 2009
Posts: 12
i keep forgetting to write back to thank people for helping me... didn't want to bring back the topic. Thanks to all!!
I'm having trouble with a program. I'm basically checking to see that a SSN is inserted correctly into this format "DDD-DD-DDDD." If its not in that format, then it returns false. My question is, what kind of method or constructor do I use to even begin? I'm having trouble finding one that points me into a direction. My initial thought is to have a method where it checks whether the fourth and seventh character has "-" and make sure the string has a length of 11 characters. But its the method to check for this "-" that i don't know what to do. I've looked at all the java.lang.String, Character, and StringBuilder and none of them seems to fit. A moose kick in the right direction will help out a lot so i could look into it!

Thanks!
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Mel:

This looks like a job for regular expressions.

John.
Mel Ram
Greenhorn

Joined: Sep 30, 2009
Posts: 12
John de Michele wrote:Mel:

This looks like a job for regular expressions.

John.


Thanks John! I feel you should be wearing a cape. The wheels are definitely turning!!! Now let me ask you this or anyone who is reading... the problem states that the user inputs the SSN number... and it also says to assume that 'D' is an interger. I am not sure if he was hinting at something there... is there a way to recognize the 'D' as any integer?

As an example: "434-23-4953".matches("DDD-DD-DDDD"); where 'D' can be any integer. Is there a method that will do that? or is there another way to approach it!?

***** Thinking to myself ***** wait... what if i used the replaceAll method to replace any integer to the letter 'D' and then use the matches method to to compare both strings....


HA!!! I DID IT !!! lol... wow... that was pretty easy!! thanks again John!!!

Problem... I am not seeing whats wrong here. if i put in a set string, it returns true. however from the user's input, it returns false. ummm... why?

Assume the user input 234-23-4224:

ssn.replaceAll("[0123456789]", "D");
System.out.println(ssn.matches("DDD-DD-DDDD"));
FALSE

String test = "123-34-2345".replaceAll("[0123456789]", "D");
System.out.println(test.matches("DDD-DD-DDDD"));
TRUE

UPDATE: Got it to work!
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 917

I think the problem can still be optimized into a single line of code...
I do need to research on regex myself to come up with a solution
I do not guarantee the following would work:



My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Salvin:

A cleaner regex would be:

John.
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 917

kool

thanks for the info,

my regex was a bit rusty (it still is)
but I thought of posting anyways
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with String class
 
Similar Threads
SimpleUniverse - clip planes
minimax algorithm?!?!
problem with html:select property and HashMap
BeanContext troubles...
Few Doubts about encoding