Java loop to collect the second and third elements every three in an array

Posted by mhollander38 on Stack Overflow See other posts from Stack Overflow or by mhollander38
Published on 2011-01-30T14:53:07Z Indexed on 2011/01/30 15:25 UTC
Read the original article Hit count: 151

Filed under:
|
|

I have a file with data in the form timestamp, coordinate, coordinate, seperated by spaces, as here;

14:25:01.215 370.0 333.0

I need to loop through and add the coordinates only to an array. The data from the file is read in and put into as String[] called info, from split(" "). I have two problems, I think the end of the file has a extra " " which I need to lose appropriately and I also want confirmation/suggestions of my loop, at the moment I am getting sporadic out of bounds exceptions. My loop is as follows;

String[] info;
info = dataHolder.split(" ");
ArrayList<String> coOrds1 = new ArrayList<String>();
for (int counter = 0; counter < info.length; counter = counter+3)
{
    coOrds1.add(info[counter+1]);
    coOrds1.add(info[counter+2]);
}

Help and suggestions appreciated.

The text file is here but the class receives in a UDP packet from another class so I am unsure if this potentially adds " " at the end or not.

© Stack Overflow or respective owner

Related posts about java

Related posts about loops