• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Splitting a String

 
Martin Vietor
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone, long time no see.

I am writing a method that is supposed to check whether or not someone is over 35. The method is given a birth date as a String (DD.MM.YYYY). This is exported from a different program, so I can't change that part. The method should return true, if the date given represents someone over 35

I would like to split that String using the "." as a separator and then parse the numbers to ints, making them comparable to the current date. Anyone turning 35 this quarter of the year will already be considered 35, even if the exact date is yet to come.

Here is the code so far:



My problem is that birhdateArray comes back empty. I would expect a String[3] with the values (for example) birthdateArray[0]==24, birthdateArray[1]==5, and birthdateArray[2]==1963 if birhtdate was set to "24.05.1963". Instead it is String[0] with the value [].

Where am I wrong?

Thank you for your help,

Martin

P.S.: In reading this I realize that currMonth and currYear are redundant. I will remove them, but they do not concern the question.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String#split takes regular expression as input argument . in regular expression dot(.) means any character . so you need to escape the dot character. some thing like
split("\\.");

hope this helps
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Change the pattern in the split - "\\." instead of "."
Dot in regexp matches any character.

Also it might be a good idea to add some checks to prevent bad input to your function, something like this:
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ireneusz Kordal wrote:Also it might be a good idea to add some checks to prevent bad input to your function, something like this:

Why?

If this was done using dates in the first place, instead of using regex and conversions etc. this would be an unnecessary step.

Personally, I'd just stick with using dates and date math.
 
Campbell Ritchie
Sheriff
Posts: 48386
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As an alternative, find classes like SimpleDateFormat, tell them to use DD.MM.YYYY and get the date directly from the String.
 
Martin Vietor
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did not know that, thank you! Like I said, the date format comes from outside. I am a physician, and it get's exported by my software at the office in this format. So I figured 2 or 3 more lines in the checkAge method are easier than changing the format.

This will solve my problem, though, so thanks everybody!
M
 
Campbell Ritchie
Sheriff
Posts: 48386
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic