0

Here's a piece of text I'm trying to work with:

lat="52.336575" lon="6.381008">< time>2016-12-19T12:12:27Z< /time>< name>Foto 8 </name>< desc>Dag 4 E&amp;F 
Geb 1.4 
Hakhoutstoof < /desc>< /wpt>

I'm trying to extract the coördinates between the "" and put the values between the "" into a string, but I can't get it to work...

Here's my code (so far):

public void openFile() {
    Chooser = new JFileChooser("C:\\Users\\danie\\Desktop\\");
    Chooser.setAcceptAllFileFilterUsed(false);
    Chooser.setDialogTitle("Open file");
    Chooser.addChoosableFileFilter(new FileNameExtensionFilter("*.gpx", 
    "gpx"));
    int returnVal = Chooser.showOpenDialog(null);

    try {
        Dummy = new Scanner(Chooser.getSelectedFile());
    } catch (FileNotFoundException E) {
        System.out.println("Error: " + E);
    }
}

public void createDummy() {
    Dummy.useDelimiter("<wpt");
    if (Dummy.hasNext()) {
        String Meta = Dummy.next();
    }
    Dummy.useDelimiter("\\s[<wpt]\\s|\\s[</wpt>]\\s");
    try {
        while (Dummy.hasNext()) {
            String Test = Dummy.next();
            DummyFile = new File("Dummy.txt");
            Output = new PrintWriter(DummyFile);
            Output.print(Test);
            Output.println();
            Output.flush();
            Output.close();          
        }

        Reader = new FileReader(DummyFile);
        Buffer = new BufferedReader(Reader);
        TestFile = new File("C:\\Users\\danie\\Desktop\\Test.txt");
        Writer = new PrintWriter(TestFile);
        String Final;
        while ((Final = Buffer.readLine()) != null) {
            String WPTS[] = Final.split("<wpt");
            for (String STD:WPTS) {
                Writer.println(STD);
                Writer.flush();
                Writer.close();
            }               
        }

    } catch (IOException EXE) {
        System.out.println("Error: " + EXE);
    }
    Dummy.close();
    }
}

I'm really new to Java :(

Charles
  • 1,121
  • 19
  • 30
  • See this question, it should point you in the right direction: https://stackoverflow.com/questions/1473155/how-to-get-data-between-quotes-in-java – Nick Clark Aug 03 '17 at 20:44
  • Read about regular-expressions with groups, that should do the trick in a neat way. And use [Regex101](http://regex101.com) to test your pattern. – SHG Aug 03 '17 at 20:44
  • 2
    That text looks awfully close to XML. If it is, it should be parsed using an XML parser, not using regex. – Andreas Aug 03 '17 at 20:57
  • You could split the string using the quote char as what you split around, then get the strings at position 1 and 3 of the array it gives you. – Rabbit Guy Aug 03 '17 at 21:12

1 Answers1

0

I think the following code will do the trick ... the "string" is only used to test the regex

    final String string = "lat=\"52.336575\" lon=\"6.381008\">< time>2016-12-19T12:12:27Z< /time>< name>Foto 8 </name>< desc>Dag 4 E&amp;F \nGeb 1.4 \n" + "Hakhoutstoof < /desc>< /wpt>";

    final String latitudeRegex = "(?<=lat=\")[0-9]+\\.[0-9]*";
    final Pattern latitudePattern = Pattern.compile(latitudeRegex);
    final Matcher latitudeMatcher = latitudePattern.matcher(string);

    //finds the next (in this case first) subsequence matching the given regex
    latitudeMatcher.find();
    String latitudeString = latitudeMatcher.group();
    double lat = Double.parseDouble(latitudeString); //group returns the match matched by previous match
    System.out.println("lat: " + lat);

to get the longitude, just replace lat by lon in the regex

this site is very useful for creating a regex https://regex101.com/ you can even create the java code at this site

JavaMan
  • 1,142
  • 12
  • 22