• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Help with String class

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Salvin:

A cleaner regex would be:

John.
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You'll never get away with this you overconfident blob! The most you will ever get is this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic