Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with String class

 
Mel Ram
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mel:

This looks like a job for regular expressions.

John.
 
Mel Ram
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1274
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Salvin:

A cleaner regex would be:

John.
 
salvin francis
Bartender
Posts: 1274
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kool

thanks for the info,

my regex was a bit rusty (it still is)
but I thought of posting anyways
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic