This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Splitting a String 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 "Splitting a String" Watch "Splitting a String" New topic
Author

Splitting a String

Martin Vietor
Ranch Hand

Joined: Oct 31, 2008
Posts: 40
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.

The problem people have in trying to create something foolproof is that they generally underestimate the inginuity of fools.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Jun 21, 2008
Posts: 423
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Oct 13, 2005
Posts: 38033
    
  22
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

Joined: Oct 31, 2008
Posts: 40
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

Joined: Oct 13, 2005
Posts: 38033
    
  22
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Splitting a String
 
Similar Threads
Turn Based Strategy Games
Need to change Calendar to dd/mm/yyyy format
Boolean value not changing
Array lists syntax in jsp
CODE: Dynamic Sorting on Multiple Indexes