1

While splitting the String using ',' I am getting Array out of bound exception. Please find below the program.

public static void main(String[] args) {
    String c="1,10k ABC D, XYZ AB,,,,,,12345,,,,,,,,,,,,,,,,,,,,,,,,,,,,,";
    System.out.println(c.split(",")[11]);
}

Even the 11th element is null i want to print the null string (Because in some records the 11th element is not null). Kindly help me to debug the error.

1
  • You could print the array c.split(",") returns: [1, 10k ABC D, XYZ AB, , , , , , 12345] Commented May 3, 2015 at 16:12

9 Answers 9

3

Probably you want

c.split(",", -1);

This will keep empty strings at the end.

Sign up to request clarification or add additional context in comments.

Comments

2

Take a look at split method description in String class:

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

So, split get rid of trailing empty strings from the array. In your example, the resulting array is:

'1'
'10k ABC D'
' XYZ AB'
''
''
''
''
''
'12345'

If you would split following string (with a space before last comma):

"1,10k ABC D, XYZ AB,,,,,,12345,,,,,,,,,,,,,,,,,,,,,,,,,,,, ,"

the resulting array would be:

'1'
'10k ABC D'
' XYZ AB'
''
''
''
''
''
'12345'
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
''
' '

Comments

1

try this will help you.

String c="1,10k ABC D, XYZ AB,,,,,,12345,,,,,,,,,,,,,,,,,,,,,,,,,,,,,";
System.out.println(c.split(",",-1)[11]);

Comments

1

Try this one!

String c="1,10k ABC D, XYZ AB,,,,,,12345,,,,,,,,,,,,,,,,,,,,,,,,,,,,,";
System.out.println(c.split(",",-1)[11]);

Comments

0

The Result of c.split(",") is a String array with 9 elements. You can view this if you use this code:

String c="1,10k ABC D, XYZ AB,,,,,,12345,,,,,,,,,,,,,,,,,,,,,,,,,,,,,";
    String[] s = c.split(",");

    for(int i = 0; i < s.length; i++){
        System.out.println(i);
        System.out.println(s[i]);
    }

If you want everything besides the ',' char you have to use ",+" instead of "," in the split. This regular expression 4 results and what it does is ignoring all the ','.

Comments

0

Suppose after splitting the generated array is splittedArray and it will be like this -

String splittedArray = {"1","10k ABC D", "XYZ" "AB","", "" "","","12345"};

You can see there is not element at splittedArray[11]. The size of the splittedArray is 9. If you fix the index 11 like this then no error will be occurred -

String c="1,10k ABC D, XYZ AB,,,,,,12345,,,,,,,,,,,,,,,,,,,,,,,,,,,,,";
System.out.println(c.split(",")[8]);

Output:
12345

Comments

0

If Java split method is applied without any argument, it discards trailing null elements. So if you debug while assigning the resulted values to an array as follows you will notice that there are only 9 elements in the resulted array which ends with element "12345".

String[] arr = c.split(",");

You can pass a limit parameter as follows (If limit parameter is non-positive then the pattern will be applied as many times as possible and the array can have any length), so that the split method will return an array with trailing null elements.

public static void main(String[] args) {
   String c="1,10k ABC D, XYZ AB,,,,,,12345,,,,,,,,,,,,,,,,,,,,,,,,,,,,,";
   System.out.println(c.split(",", -1)[11]);
}

Comments

0

Hope the below code helps

public static void main(String[] args) {
    String cvsSplitBy =",(?=([^\"]*\"[^\"]*\")*[^\"]*$)";
    String c="1,10k ABC D, XYZ AB,,,,,,12345,,,,,,,,,,,,,,,,,,,,,,,,,,,,,";
    String sTemp[] = c.split(cvsSplitBy);
    for(int i=0;i<sTemp.length;i++){
        if(((String) c.split(cvsSplitBy)[i])!=null && (!"".equals((String) c.split(cvsSplitBy)[i].trim())))
    System.out.println((String) c.split(cvsSplitBy)[i]);
    }

Output for above code

1
10k ABC D
XYZ AB
12345

Comments

0

According to split documentation :

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.

This is validated by the source code of the method split in java.util.regex.Pattern, used by the split of String

if (limit == 0)
    while (resultSize > 0 && matchList.get(resultSize-1).equals(""))
        resultSize--;

The actual size of your array is 9

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.