Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

String.split() method

807603Oct 18 2007 — edited Oct 23 2007
I have a text file that's comma delimited. It contains 3 values per line ( firstname, lastname, age)
String s = "Joe,,23";
String[] delimitedArray = s.split(",");
the problem comes when there is a value that isn't entered, like in the example lastname is not there so when you use the split method and you check the string array's length it returns 2 instead of 3, which is totally understandable. However is there a way to make sure it will always have 3 elements in the array, even if one or more values are missing? I know you could
s.split(",",3);
but even if I try get the 3rd element it will say indexOutOfBounds (again, expected).

Any work around for this?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 20 2007
Added on Oct 18 2007
18 comments
1,128 views