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