0

I have a very long string containing GPS data but this is not important. What I need to do is separate the string which is in an arraylist (one big string) into multiple pieces.

The tricky part is that the string is made up of multiple 'gps sentances' and I only require two types of these sentences.
The types I need start with $GPSGSV and $GPSGGA. Basically I need to dump ONLY THESE sentences into another arraylist while leaving all the rest behind.

The new arraylist must be in line-by-line form so that each sentence is followed by a new line.
Each sentence also ends in one white space which could be helpful when splitting up. The arraylist data is shown below. - This is printed from the arraylist.

[$GPGSA,A,3,28,09,26,15,08,05,21,24,07,,,,1.6,1.0,1.3*3A, $GPRMC,151018.000,A,5225.9627,N,00401.1624,W,0.11,104.71,210214,,*14, $GPGGA,151019.000,5225.9627,N,00401.1624,W,1,09,1.0,38.9,M,51.1,M,,0000*72, $GPGSA,A,3,28,09,26,15,08,05,21,24,07,,,,1.6,1.0,1.3*3A, $GPGSV,3,1,12,26,80,302,44,09,55,063,40,05,53,191,39,08,51,059,37*79, $GPGSV,3,2,12,28,43,112,34,15,40,284,42,21,18,305,33,07,18,057,27*7E, $GPGSV,3,3,12,10,05,153,,24,05,234,38,18,05,318,22,19,05,035,*79, $GPRMC,151019.000,A,5225.9627,N,00401.1624,W,0.10,105.97,210214,,*1D, $GPGGA,151020.000,5225.9627,N,00401.1624,W,1,09,1.0,38.9,M,51.1,M,,0000*78, $GPGSA,A,3,28,09,26,15,08,05,21,24,07,,,,1.6,1.0,1.3*3A, $GPRMC,151020.000,A,5225.9627,N,00401.1624,W,0.12,105.18,210214,,*12, $GPGSA,A,3,28,09,26,15,08,05,21,24,07,,,,1.6,1.0,1.3*3A, $GPRMC,151021.000,A,5225.9626,N,00401.1624,W,0.11,99.26,210214,,*28, $GPGGA,151022.000,5225.9626,N,00401.1623,W,1,09,1.0,38.9,M,51.1,M,,0000*7C, $GPGSA,A,3,28,09,26,15,08,05,21,24,07,,,,1.6,1.0,1.3*3A, $GPRMC,151022.000,A,5225.9626,N,00401.1623,W,0.11,109.69,210214,,*1F,

The data continues up to 2000 sentences.

Any help would be great. Thanks

EDITS ------

Looking back at what I have.. It may be best if I just read in the lines (as the file is formatted to be one sentence per line) which start with either the GSV or the GGA tag. In the buffered reader section of the method, how could I go about doing that? Here is some of my code ....

try {
    File gpsioFile = new File(gpsFile);

    FileReader file = new FileReader(gpsFile);
    BufferedReader buffer = new BufferedReader(file);
    StringBuffer stringbuff = new StringBuffer();
    String ans;


    while ((ans = buffer.readLine()) != null) {     
        gps.add(ans);
        stringbuff.append(ans);
        stringbuff.append("\n");
    }
} catch (Exception e) {
    e.printStackTrace();
}

From this could I get an Arraylist with just the GGA and GSV sentences/lines but in the same order that they were from the file? Thanks

Richard Miskin
  • 1,260
  • 7
  • 12
NJD
  • 197
  • 1
  • 4
  • 17

2 Answers2

0

OK, I'd first start by splitting your string into individual lines with spilt():

  String[] split = "$GPGSA,A,3,28,09,26,15,08,05,21,24,07,,,,1.6,1.0,1.3*3A,".split(",");

you can also use "\n" as a split delimiter instead of ",". This will give you an array over which you can iterate.

  List<String> filtered = new ArrayList<String>()
  for (String item, split) {
       if (item.startsWith("$GPGSA")) {
            filtered.add(item);
       }
  }

filtered would be a new Array with the items you want to keep.

This approach works with JDK 6+. In JDK 8, this kind of problem can be solved more elegantly with the stream API.

Erik Schmiegelow
  • 2,739
  • 1
  • 18
  • 22
  • The array is a string array and and all the data I need (sentences) do either start with $GPSGGA and $GPSGSV but I do not understand how entering the entire contents of the array into the String [] = split ="...." will work. Do you mean something such as String [] = split (arraylist name) .split ("\n"); ----- Also with the filtered List, how would the 'startsWith' operator know where to end after the $GPSGSV for example.. – NJD Mar 08 '14 at 17:36
  • OK - for starters I thought you had an a string and not a String array to begin with. split essentially cuts a long string into a string array using the delimiter as a seperator. – Erik Schmiegelow Mar 08 '14 at 17:40
  • startsWith essentially answers wether the item you're looking at matches the pattern you're looking for. if startsWith returns true, you can add that string to the new array (filtered in my example) or split into further, smaller chunks. – Erik Schmiegelow Mar 08 '14 at 17:41
0

My understanding is that you've got an ArrayList with a single String element. That String is a comma separated list of values. So step one is to extract the string and split it into it's constituent parts. Once you've done that you can process the each item in turn.

private static List<List<String>> splitData(final ArrayList<String> data) {
    final List<List<String>> filteredData = new ArrayList<List<String>>();

    String fullText = data.get(0);
    String[] splitData = fullText.split(",");

    List<String> currentList = null;
    for (int i = 0;i < splitData.length; i++) {
        final String next = splitData[i];
        if (startTags.contains(next)) {
            if (interestingStartTags.contains(next)) {
                currentList = new ArrayList<String>();
                filteredData.add(currentList);
            } else {
                currentList = null;
            }
        }
        if (currentList != null) {
            currentList.add(next);
        }
    }

    return filteredData;
}

The two static Set<String> provide the set of all 'gps sentence' start tags and also the set of ones you're interested in. The split data method uses startTags to determine if it has reached the start of a new sentence. If the new tag is also interesting, then a new list is created and added to the List<List<String>>. It is this list of lists that is returned.

If you don't know all of the strings you want to use as 'startTag' then you could next.startsWith("$GP") or similar.

Reading the file

Looking at the updated question of how to read the file you could remove the StringBuffer and instead simply add each line you read to an ArrayList. The code below will step over any lines that do not start with the two tags you are interested in. The order of the lines within lineList will match the order they are found in the file.

FileReader file = new FileReader(gpsFile);
BufferedReader buffer = new BufferedReader(file);
String ans;
ArrayList<String> lineList = new ArrayList<String>();

while ((ans = buffer.readLine()) != null) {
    if (ans.startsWith("$GPSGSV")||ans.startsWith("$GPSGGA")) {
        lineList.add(ans);
    }
}
Richard Miskin
  • 1,260
  • 7
  • 12
  • I have been trying to implement what I have been told but I am unable to see how to put the arraylist(gps) which contains all of the gps data into the splitting methods you have shown me. I dont see how I would give them the gps arraylist – NJD Mar 08 '14 at 17:52
  • Is your 'arraylist(gps)' an actual Java ArrayList? In the question you seem to have printed out the `toString()` but what is the underlying type. – Richard Miskin Mar 08 '14 at 17:56
  • If you've actually just got a single String containing all the data then you could use the `split()` method to split it on commas and then loop over the array in a similar way my suggestion above. – Richard Miskin Mar 08 '14 at 18:01
  • It is an arraylist of a single string from a text file which was properly formatted to one sentence starting with a $ on each line. I wa sunable to split them into separate strings on loading into the arraylist. – NJD Mar 08 '14 at 18:02
  • I've updated my answer to work with you suggested data. However, it sounds like you might want to look at how you're loading the data from the file. If you use a `BufferedReader` you could read one line at a time from the file. Or if the data really is a normal CSV file look to using something like [opencsv](https://github.com/jlawrie/opencsv) do the reading.. – Richard Miskin Mar 08 '14 at 19:47
  • Okay - I've looked a little further and it sounds like you're trying to write an NMEA parser. You might find interesting stuff here http://stackoverflow.com/questions/587441/roll-your-own-nmea-parser-or-use-an-open-source-gps-parser – Richard Miskin Mar 08 '14 at 19:55
  • Yes your right I am trying to do that eventually and I will look at that post now, I have made an edit to the 'post' that may help clear some things up. – NJD Mar 08 '14 at 21:40