1

I'm trying to read coordinates from a file into an array.

Every coordinate has an id, an x-value and a y-value.

The file is in the following format: id position.x position.y

Example:

292961234 1376.42 618.056
29535583 3525.73 530.522
256351971 836.003 3563.33
20992560 4179.74 3074.27

Note: There are 4 lines containing a total of 4 coordinates.

I created the class Node and its constructor expects (int id, double x, double y).

And this is the class NodeList which is supposed to have an array attribute that saves the Nodes:

package .....;

import java.io.File;
import java.util.Iterator;
import java.util.Locale;
import java.util.Scanner;

public class NodeList implements Iterable<Node> {
    private Node[] nodes = getNodes();
    @Override
    public Iterator<Node> iterator() {
        return null;
    }
    public Node[] getNodes() {
        Node[] result;
        File f;
        Scanner scanner;
        try {
            f = new File("nodes.txt");
            Scanner s = new Scanner(f);
            int ctr = 0;
            while (s.hasNextLine()) {
                ctr++;
                s.nextLine();
            }
            result = new Node[ctr];
        }
        catch (Exception e) {
            return null;
        }
        try {
            scanner = new Scanner(f);
        }
        catch (Exception e) {
            return null;
        }
        Locale.setDefault(new Locale("C"));
        for(int i = 0; i < result.length; i++) {
            int id = scanner.nextInt();
            double x = scanner.nextDouble();
            double y = scanner.nextDouble();
            result[i] = new Node(id,x,y);
        }
        return result;
    }
    public static void main(String[] args) {
        NodeList nl = new NodeList();
    }
}

The Node class:

package ...;

public class Node {
    private int id;
    private double x;
    private double y;
    public Node(int id, double x, double y){
        this.id = id;
        this.x = x;
        this.y = y;
    }
    public int getId(){
        return id;
    }
    public double getX(){
        return x;
    }
    public double getY(){
        return y;
    }
}

When the method containing the shown code is called, I get the following Exception:

Exception in thread "main" java.util.InputMismatchException
    at java.base/java.util.Scanner.throwFor(Scanner.java:939)
    at java.base/java.util.Scanner.next(Scanner.java:1594)
    at java.base/java.util.Scanner.nextDouble(Scanner.java:2564)
    at ......NodeList.getNodes(NodeList.java:40)
    at ......NodeList.<init>(NodeList.java:9)
    at ......NodeList.main(NodeList.java:47)

Process finished with exit code 1

Link to the file containing the nodes: https://pastebin.com/zhzp3DTi

  • What is `scanner`? Are you trying to use `Scanner` object to read a file? Also, try using a comma instead of a dot on your double expressions. – Gaurav Mall Aug 15 '19 at 11:54
  • 1
    Possible duplicate of [Java: How to read a text file](https://stackoverflow.com/questions/2788080/java-how-to-read-a-text-file) – rajah9 Aug 15 '19 at 11:54
  • Possible duplicate of https://stackoverflow.com/questions/36912470/why-is-nextdouble-from-the-scanner-method-sending-me-exception – JP Moresmau Aug 15 '19 at 11:54
  • Welcome to StackOverflow. There's a related answer for reading just integers at https://stackoverflow.com/a/2788114/509840. Do you think you could modify your code to read an integer and a double? – rajah9 Aug 15 '19 at 11:56
  • @GauravMall scanner is a Scanner object that reads from my file. Our teachers gave us a file where they used dots so I guess I have to stick with that. –  Aug 15 '19 at 12:01
  • I got that. The error that you are up against is actually that of a locale – Gaurav Mall Aug 15 '19 at 12:02
  • Check out the duplicate links provided by the users. – Gaurav Mall Aug 15 '19 at 12:03
  • Can you post the node class too? Because it is showing that the error is caused by `NodeList.getNodes()` – Gaurav Mall Aug 15 '19 at 12:41
  • It looks like your input file is corrupted can you post the exact content of your input file? – Stephan Hogenboom Aug 15 '19 at 12:57
  • @StephanHogenboom It's more than 8000 lines, are you sure? –  Aug 15 '19 at 13:00
  • Can you post a link to the file? – Stephan Hogenboom Aug 15 '19 at 14:16
  • @StephanHogenboom I added the link at the end of my question. –  Aug 15 '19 at 18:22
  • I tested it with the file it seems to work perfectly. I think you need to post your entire class, because we cannot reproduce your problem. – Stephan Hogenboom Aug 16 '19 at 09:08
  • @StephanHogenboom I added the full class to my question. –  Aug 21 '19 at 07:30
  • @Jumpman i ran your full code example with the pastebin data and it works just fine. Im afraid something is wrong with the localdata you are processing. The error message sais you arw parsing a field that is either not an int or double as a double or int – Stephan Hogenboom Aug 21 '19 at 14:04
  • @StephanHogenboom What is the localdata? –  Aug 23 '19 at 08:31

1 Answers1

0

It might be that it is using wrong locale. I don't think "C" is a valid locale, it should be a language code.

You can try to use

scanner.useLocale(Locale.ROOT);

This is working on my machine:

public static void main(String[] args) {
    // TODO code application logic here
    String s = "292961234 1376.42 618.056\n" +
               "29535583 3525.73 530.522\n" +
               "256351971 836.003 3563.33\n" +
               "20992560 4179.74 3074.27";
    Scanner scanner = new Scanner(s);
    scanner.useLocale(Locale.ROOT);
    for(int i = 0; i < 4; i++) {
        int id = scanner.nextInt();
        double x = scanner.nextDouble();
        double y = scanner.nextDouble();
        System.out.println(id+" "+x+" "+y);
    }
}