tags:

views:

656

answers:

5

Hi All : I am using the following code:

String sample = "::";
String[] splitTime = sample.split(":");
// extra detail omitted
System.out.println("Value 1 :"+splitTime[0]);
System.out.println("Value 2 :"+splitTime[1]);
System.out.println("Value 3 :"+splitTime[2]);

I am getting ArrayIndexOutofBound exception. How does String.split() handle consecutive or trailing / opening delimiters?

See also:

A: 

Use the function StringTokenizer in which u pass the string and the second argument as delimiter

use splittime.length function to find the length

Jugal
Bad advice. StringTokenizer would treat the two colons as one delimiter. Anyway, StringTokenizer is a legacy class; developers are encouraged to use split() instead.
Alan Moore
About using splitTime.length: in this example the length will always be zero. All of the "tokens" would have been zero-length strings, and StringTokenizer never returns empty tokens.
Alan Moore
+2  A: 

From the J2SE API manual:

Trailing empty strings are therefore not included in the resulting array.

So, if you pass in "::" you'll get an empty array because all of the delimiters are trailing.

If you want to make sure that you get no more than three entries you should use:

String[] splitTime = sample.split(":", 3);

With an input of "::" that would indeed give you three empty strings in the output array.

However if the input only happens to have one ":" in it then you'll still only get two elements in your array.

Alnitak
using "::".split(":") actualy returns an empty array and not a two element array.
Carlos Heuberger
indeed, you're right - I'll edit accordingly.
Alnitak
A: 

Hi,

you should check the length of the splitTime array.

+4  A: 

Alnitak is correct that trailing empty strings will be discarded by default.

If you want to have trailing empty strings, you should use split(String, int) and pass a negative number as the limit parameter.

The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter. If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.

Note that split(aString) is a synonym for split(aString, 0):

This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.

Also, you should use a loop to get the values from the array; this avoids a possible ArrayIndexOutOfBoundsException.

So your corrected code should be (assuming you want the trailing empty strings):

String sample = "::";
String[] splitTime = sample.split(":", -1);
for (int i = 0; i < splitTime.length; i++) {
    System.out.println("Value " + i + " : \"" + splitTime[i] + "\"");
}

Output:

Value 0 : ""
Value 1 : ""
Value 2 : ""
Michael Myers
good point about the -ve limit, but still requires that the array bounds are checked in case the input only had one ":" in it.
Alnitak
Point taken.
Michael Myers
+1  A: 

Like this perhaps?

int ndx = 0;
StringTokenizer t = new StringTokenizer(": : ::::",":");
while (t.hasMoreElements())
{
    System.out.println(String.format("Value %d : %s", ++ndx,t.nextElement()));
}
Matthew Sposato
That returns two tokens, each containing just a space. But Raja specifically asked about strings that contain nothing but delimiters
Alan Moore
A couple more points. 1. when you use a StringTokenizer you should always use hasMoreTokens() and nextToken(), not hasMoreElements() and nextElement(). Those are only there to flesh out the Enumeration interface. 2. There's no need to need to use String.format() the way you did; PrintStreams like System.out have their own format() methods, plus printf() methods for the traditionalists. :-)
Alan Moore
Thanks Alan,As you may have guessed already, java is not my primary language. However I'm trying to become more proficient with it. So I appreciate your comments.
Matthew Sposato