How should I read coordinates from a text file? I have a text file with lines that look like this:

Robot(479.30432416307934|98.90610653676828)
Robot(186.42081184420528|213.11277688981409)
Robot(86.80794277768825|412.1359734884495)


or, more general:

Robot(DOUBLE|DOUBLE)


How should I parse it in Java?

I have written the following (tested and working) code:

private static List&lt;Position&gt; getPositions(File myFile)
        throws FileNotFoundException, IOException {
    List&lt;Position&gt; result = new ArrayList();
    BufferedReader br = new BufferedReader(new FileReader(myFile));
    try {
        String line = br.readLine();
        while (line != null) {
            String coordinates = line.substring(6, line.length() - 1);
            int splitpoint = coordinates.indexOf('|');
            double x = Double.parseDouble(coordinates.substring(0,
                    splitpoint));
            double y = Double.parseDouble(coordinates
                    .substring(splitpoint + 1));
            result.add(new Position(x, y));
            line = br.readLine();
        }
    } finally {
        br.close();
    }
    return result;
}


It looks a too complicated for such a simple task for me. I mean, with the RegEx 

Robot\(\d+\.\d+|\d+\) 


I can match a line. According to Regextester my regex is correct. But how can I get the interesting part (the two doubles) from the regex?

Result after answer

/**
 * Read a file with positions of roboters.
 * @param file the file with positions
 * @return the list with roboter positions
 * @throws FileNotFoundException
 * @throws IOException
 */
private static List&lt;Position&gt; getPositions(final File file)
        throws FileNotFoundException, IOException {
    if (file == null || !file.canRead()) {
        throw new IllegalArgumentException("file not readable: " + file);
    }

    final Scanner s = new Scanner(file).useDelimiter("Robot\\(|\\||\\)\n?");
    final List&lt;Position&gt; positions = new ArrayList&lt;Position&gt;();
    while (s.hasNext()) {
        positions.add(new Position(s.nextDouble(), s.nextDouble()));
        s.nextLine();
    }

    return positions;
}
As this is posted on codereview, let us take a look at the whole function.

private static List&lt;Position&gt; getPositions(File myFile)
        throws FileNotFoundException, IOException {


I would suggest to do a check for the file (and rename it to file, I do not think there is a herFile, hisFile and so on):

    if (file == null || !file.canRead())
        throw new IllegalArgumentException("file not readable: " + file);


Which could make error handling a bit easier. We will see later.



    List&lt;Position&gt; result = new ArrayList();
    BufferedReader br = new BufferedReader(new FileReader(myFile));
    try {
        String line = br.readLine();
        while (line != null) {
        ...
            line = br.readLine();


I would keep the typical pattern, so everyone can recognize it:

        String line;
        while ((line = bufferedReader.readLine()) != null) {




            String coordinates = line.substring(6, line.length() - 1);
            int splitpoint = coordinates.indexOf('|');
            double x = Double.parseDouble(coordinates.substring(0, splitpoint));
            double y = Double.parseDouble(coordinates.substring(splitpoint + 1));
            result.add(new Position(x, y));
            line = br.readLine();
        }


For me, this is nearly fine. Just to a split("|") and you are done in the easy way:

            final String[] coordinatesArray = line.substring(6, line.length() - 1).split("|");
            final double x = Double.parseDouble(coordinatesArray[0]);
            final double y = Double.parseDouble(coordinatesArray[1]);
            result.add(new Position(x, y));


And I suggest to add an example in a comment, to help the next reader.



    } finally {
        br.close();
    }


If you have Java7, you could change this to try-with statement:

    try (final BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) {
        ...
    }        


And you could catch the error. The most problematic ones are cought by the file checks. Everything else is probably something you can not do anything about.
Then it is fine to catch it, show it and continue:

    } catch (NumberFormatException | IOException e) {
        e.printStackTrace(); // handle the exception or rethrow it. It could be safe to do nothing here
    }




If you put everything together, it could look like this:

private static List&lt;Position&gt; getPositions(final File file) {
    if (file == null || !file.canRead())
        throw new IllegalArgumentException("file not readable: " + file);

    final List&lt;Position&gt; result = new ArrayList&lt;&gt;();
    try (final BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) {
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            // example: Robot(479.30432416307934|98.90610653676828)
            final String[] coordinatesArray = line.substring(6, line.length() - 1).split("|");
            final double x = Double.parseDouble(coordinatesArray[0]);
            final double y = Double.parseDouble(coordinatesArray[1]);
            result.add(new Position(x, y));
        }
    } catch (NumberFormatException | IOException e) {
        e.printStackTrace(); // handle the exception or rethrow it. It could be safe to do nothing here
    }
    return result;
}




If you try the way with the scanner, you shoudl just tell him which characters to ignore. Please do not try a complex regular expression. They are unreadable.
What we have there? 


  Robot(479.30432416307934|98.90610653676828)


Well, everything which is not a number or a point. To save some typing, we do not place every character alone, we put them together as \p{Alpha} (any alphabetic character): [\p{Alpha}()|\]. And we want this to happen for one or more times: [\p{Alpha}()|]+. 
And we should not forget about multiple lines, then there is a newline: [\p{Alpha}()|\\n]+.  

It is still mostly readable if you have a basic understanding of regular expressions, so we are fine in this case. You could also use [a-zA-Z()|\\n]+ which would be more common outside the java world.

The complete function could look like this:

private static List&lt;Position&gt; getPositions(final File file) {
    if (file == null || !file.canRead())
        throw new IllegalArgumentException("file not readable: " + file);

    final List&lt;Position&gt; result = new ArrayList&lt;&gt;();

    try (Scanner scanner = new Scanner(file)) {
        // example: Robot(479.30432416307934|98.90610653676828)
        scanner.useDelimiter("[\p{Alpha}()|\\n]+");
        while (scanner.hasNext())
            result.add(new Position(scanner.nextDouble(), scanner.nextDouble()));
    } catch (final FileNotFoundException e1) {
        e1.printStackTrace(); // handle the exception or rethrow it. It could be safe to do nothing here
    }
    return result;
}