0

Why does the split method not return an array with 2 elements?

for(int i = 0; i < temparray.size(); i++)
{
if (temparray.get(i).contains("_"))
    System.out.println("True" + temparray.get(i).length() + " " + temparray.get(i));
String[] temp = temparray.get(i).split("_");
System.out.println(temp[0]);
//System.out.println(temp[1]);
//friendsOld.add(new Friend(temp[0],temp[1]));
}

If I uncomment either of the lines, I get ArrayOutofBoundsException: 1. The println always returns True, the length of the String, and then a String with _ located within the String - NOT at the end.

I've tried negative parameters for .split(), converting the String to char arrays and breaking the String using indexOf() to find the location of _ then splitting it manually using substring(). There might be something wrong with the String itself but here is the code for the array of Strings: ArrayList<String> temparray = new ArrayList<String>();.

6
  • 2
    what is the exact string value stored in temparray.get(i)? Commented Mar 13, 2012 at 14:40
  • What String are you applying that code to? Show us some examples so it'll be easier to help you find a solution. Commented Mar 13, 2012 at 14:41
  • As others mentioned it already: you might have forgot some braces. But also be aware of that trailing empty strings are not included, so the string "foo_" would also return only one element even though it contains your seperator (see: docs.oracle.com/javase/1.4.2/docs/api/java/lang/…) Commented Mar 13, 2012 at 14:44
  • Here are examples: Yarko Berlinh_40656411 Joseph Nguyen_52233247 Johan Cheah_72681788 @AmitBhargava Commented Mar 13, 2012 at 14:44
  • @JoachimRohde, the _ is never at the end so the trailing string isn't empty. Correct? Commented Mar 13, 2012 at 14:46

3 Answers 3

8

It seems that you forgot the braces after the if-statement:

if (temparray.get(i).contains("_")) {
    System.out.println("True" + temparray.get(i).length() + " " + temparray.get(i));
    String[] temp = temparray.get(i).split("_");
    System.out.println(temp[0]);
    System.out.println(temp[1]);
    friendsOld.add(new Friend(temp[0],temp[1]));
}

The way you wrote it, the string is splitted even when it doesn't contain an underscore. Only the output of "True [...]" is limited to strings containing one.

You should start using the debugger - it will display the values of variables when hitting an exception breakpoint allowing you to further track down the bugs in your code.

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

3 Comments

I didn't intend for it to have braces. The if-statement was simply meant to make sure that the String was valid for "splitting". Each element of temparray returns True for the contains().
Are you really sure that every element contains and underscore (not only the ones you are printing to the console)? On which element does the exception occur?
Your comment made me realize something and upon adding another inStream.readLine(); it worked correctly. I was reading the wrong line first, one off. Thank you.
4

Did you mean to put all of that code in braces?

for(int i = 0; i < temparray.size(); i++)
{
    if (temparray.get(i).contains("_")) {
        System.out.println("True" + temparray.get(i).length() + " " + temparray.get(i));
        String[] temp = temparray.get(i).split("_");
        System.out.println(temp[0]);
        //System.out.println(temp[1]);
        //friendsOld.add(new Friend(temp[0],temp[1]));
    }
}

1 Comment

I didn't intend for it to have braces. The if-statement was simply meant to make sure that the String was valid for "splitting". Each element of temparray returns True for the contains().
3

Your if condition only applies to the next line. Therefore, if the temparray.get(i) does not contain a '_', you only get a single result from split.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.