0

I trying to get a specific data from a line that using ! as split. For example:

S!ARC!256!547291!840.50!9038.00!840.50!9038.00!519.50!9038.00!321.00!0.00!COUNTERCLOCKWISE!ASSEMBLY_TOP!BAT-2-CR1632-T0_R!BT2K!

so when i split the line using ! and put it in array, the array should contain 16 data. But i facing problem that when data like

S!LINE!257!37825 1!3525.00!10720.00!4525.00!10720.00!6.00!!!!!ASSEMBLY_TOP!CSCO-LABEL-ASY-73-SN-COMB_R!! 

and it ignore the last data which is null or "". So when i try to take the data[15] , the problem ArrayIndexOutOfBoundsException keep pop out.Below are some code that i write:

String[] data = $LINE_LIST.get(k).split("!");                     
if ($LINE_LIST.get(k).startsWith("S!") &&  $LINE_LIST.get(k).contains("ASSEMBLY_TOP"))
                    { 

                            $GRAPHIC.append(data[15])
                                .append("!")
                                .append(data[14])
                                .append("!")
                                .append("TOP")
                                .append("\n");
                    }

Any way to solve this problem??

3
  • 3
    For the first time I'm seeing a variable name starts with $? Commented Sep 21, 2015 at 6:56
  • poor formatting. Please improve. Commented Sep 21, 2015 at 6:56
  • It was a String builder for $GRAPHIC and $LINE_LIST is a ArrayList Commented Sep 21, 2015 at 6:57

2 Answers 2

2

split eliminates trailing empty Strings. Therefore, in your example the length of the array would be 15, so data[15] is out of bounds.

0 "S"
1 "LINE"
2 "257"
3 "37825 1"
4 "3525.00"
5 "10720.00"
6 "4525.00"
7 "10720.00"
8 "6.00"
9 ""
10 ""
11 ""
12 ""
13 "ASSEMBLY_TOP"
14 "CSCO-LABEL-ASY-73-SN-COMB_R"

You must check that data.length > 15 before accessing data[15].

Here's a relevant quote from the Javadoc of split :

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.

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

2 Comments

so i should add 1 more condition in my if to look like this? if ($LINE_LIST.get(k).startsWith("S!") && $LINE_LIST.get(k).contains("ASSEMBLY_TOP") && data.length>15)
@Jian.Eoh You should also make sure that data[14] is valid. It is in your sample input, but I don't know if it's valid for any input.
0

try this:

String s="S!LINE!257!37825 1!3525.00!10720.00!4525.00!10720.00!6.00!!!!!ASSEMBLY_TOP!CSCO-LABEL-ASY-73-SN-COMB_R!! ";
        String arr[]=s.split("!"); // split line with delimiter !
        List<String> list = new ArrayList<String>(Arrays.asList(arr));
        list.removeAll(Arrays.asList("", null)); // remove empty entry
        System.out.println(list); // now list contains your desired data

Now you can access specific data from a list

[S, LINE, 257, 37825 1, 3525.00, 10720.00, 4525.00, 10720.00, 6.00, ASSEMBLY_TOP, CSCO-LABEL-ASY-73-SN-COMB_R,  ]

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.