3
votes

I am working on android. I have a string like below.

String str = "@test1|test2|test3@test4|test5|test6|test7@test8|test9|test10|test11@test12|test13|test14|test15@......"

I am splitting the above string with "@" using String[] str1 = str.split("[@]"); so that the final results are

str1[0] = test1|test2|test3

str1[1] = test4|test5|test6|test7

...

The code snippet I used

   for (int x = 1; x < str1.length; x++) {
    String[] st1 = str1 [x].trim().split("\\|");
    System.out.println("first" + st1[0].trim());
    System.out.println("second" + st1[1].trim());
    System.out.println("third" + st1[2].trim());
    System.out.println("fourth" + st1[3].trim());  

   List1.add(st1[0].trim());
   List2.add(st1[1].trim());
   List3.add(st1[2].trim());
   List4.add(st1[3].trim());

    }

In the above for loop, when the loop starts from x=2 it is working fine. But if I give x=1 then it is throwing array index out of bounds exception at "System.out.println("fourth" + st1[3].trim());". Is because str1[0] consists of only 3 items whereas the remaining consists of 4 items. So now I am unable to get the fourth value after splitting with "|". Please tell me how to get the fourth value. Thanks in advance.

5

5 Answers

3
votes

ArrayIndexOutOFBound is coming because you are accessing the forth value you can get by following two ways,

String[] str1 = str.split("@");
        for (int x = 1; x < str1.length; x++) {
            String[] st1 = str1[x].trim().split("\\|");
            for (int j = 0; j < st1.length; j++) {
                System.out.println(st1[j]);

            }

        }

or

String[] str1 = str.split("@");
        for (int x = 1; x < str1.length; x++) {
            String[] st1 = str1[x].trim().split("\\|");
            /*for (int j = 0; j < st1.length; j++) {
                System.out.println(st1[j]);

            }*/
            if(st1.length> 0 )
                System.out.println("first" + st1[0].trim());
            if(st1.length> 1 )
                System.out.println("second" + st1[1].trim());
            if(st1.length> 2 )
                System.out.println("third" + st1[2].trim());
            if(st1.length> 3 )
                System.out.println("fourth" + st1[3].trim());

        }
0
votes

You have @ in your string I don't see [@] any where, so why are you splitting the string with this token.

Change

String[] str1 = str.split("[@]")

to

String[] str1 = str.split("@")

For solving indexOutOfBounds:

for (int x = 1; x < str1.length; x++) {
  String[] st1 = str1 [x].trim().split("\\|");
  for (int j = 0; j < st1.length; j++)
  {
      System.out.println( j "= " + st1[j].trim());
  }

}
0
votes
String str = "@test1|test2|test3@test4|test5|test6|test7@test8|test9|test10|test11@test12|test13|test14|test15@";
        String[] str1 = str.split("[@]");

        for (int x = 0; x < str1.length; x++) {
            String[] str2 = str1[x].trim().split("\\|");
            for (int y = 0; y < str2.length; y++) {
                System.out.println(str2[y].trim());
            }
        }

Output:

test1
test2
test3
test4
test5
test6
test7
test8
test9
test10
test11
test12
test13
test14
test15

Hope this will help you.

0
votes
for (int x = 1; x < str1.length; x++) {
String[] st1 = str1 [x].trim().split("\\|");
System.out.println("first" + st1[0].trim());
System.out.println("second" + st1[1].trim());
System.out.println("third" + st1[2].trim());
System.out.println("fourth" + st1[3].trim());

}

Change to:

for (int x = 0; x < str1.length; x++) {
String[] st1 = str1 [x].trim().split("\\|");
for (int y = 0; y < st1.length; y++)
{
System.out.println("value at "+String.ValueOf(y)+": " + st1[y].trim());
}

}
0
votes

You have IndexOutOfBoundException because the first substring (str1[0]) only has 3 items and you tried to access the 4th item. This code snippet should work.

String[] str1 = str.split( "[@]" );
List<String> list = new ArrayList<String>();
for ( int x = 1; x < str1.length; x++ )
{
    String[] st1 = str1[ x ].trim().split( "\\|" );
    for ( int index = 0; index < st1.length; index++ )
    {
        String value = st1[ index ].trim();
        System.out.println( "value in index " + index + "=" + value );
        list.add( value );
    }
 }