I am currently writing a dissertation for a bachelor's degree in graph theory and use a java scanner to convert a txt file with edges into my graph in a Java class. My txt file looks like this:
1 2 72 3
2 3 15 98
4 7 66 49
5 6 39 48
6 9 87 97
8 13 31 5
The results are sorted as: initial peak, drain peak, cost, capacity.
My code is as follows:
Graph graph = new Graph(false); File f = new File("Filepath"); Scanner in = new Scanner(f); while (in.hasNextLine()) { for (int i =1; i<= numberEdges; i++) { String s = in.nextLine(); try (Scanner inscan = new Scanner(s)) { while (inscan.hasNext()) { int source = inscan.nextInt(); int sink = inscan.nextInt(); double cost =inscan.nextDouble(); double capacity = inscan.nextDouble(); Vertex Source = new Vertex(source); Vertex Sink = new Vertex(sink); Edge edge = new Edge(Source,Sink, cost, capacity); graph.addEdge(edge); } } } } in.close();
I tried to scan each string into a String and then scan the String into my variables. It always throws a “NoLineFound” exception in the first line of the for loop, and if I try it with line output, I don't get any. But when I turn off the second scanner and try again, I get all the lines in the output, but at the end the exception is “NoLineFound”.
I checked my txt file and the last line does not have the end of the UTF8 line, but I do not know how to do this.
source share