I am trying to implement Dijkstra Algorithm that reads a DOT file (.txt) and should give back a map and first of all i have to extract Data from a textfile to an ArrayList, so I made two classes one for the Verticles, and the other Class for the Edges.
the text file looks like this :
Digraph {
A -> B [label="10,90"];
A -> C [label="8,80"];
C -> B [label="1,50"];
B -> D [label="7,60"];
C -> D [label="6,80"];
D -> E [label="4,90"];
E -> F [label="2,130"];
D -> F [label="5,130"];
F -> G [label="5,120"];
G -> H [label="5,100"];
A [label="A,5"];
B [label="B,4"];
C [label="C,3"];
D [label="D,2"];
E [label="E,1"];
F [label="F,6"];
G [label="G,7"];
H [label="H,8"];
}
for example in line 1 I should extract the Source which is "A" and the Destination which is "B" and the speedLimit which is 10 and the distance which is 90 this is concerning the edges. and from line 11 I should extract the name and time for example in the line 11 the name should be "A" and the time should be 5.
here is my simple first two classes :
VerticlesRep Class :
package lab;
public class VerticesRep {
private String name;
private int time;
public VerticesRep(String name, int time) {
this.name = name;
this.time = time;
}
public String getName() {
return name;
}
public int getTime() {
return time;
}
public void setName(String name) {
this.name = name;
}
public void setTime(int time) {
this.time = time;
}
}
and here is my EdgesRep Class :
package lab;
public class EdgesRep {
private String Source;
private String Destination;
private int speedLimit;
private int distance;
public EdgesRep(String Source, String Destination, int speedLimit, int distance) {
this.setSource(Source);
this.setDestination(Destination);
this.setSpeedLimit(speedLimit);
this.setDistance(distance);
}
public String getSource() {
return Source;
}
public void setSource(String source) {
Source = source;
}
public String getDestination() {
return Destination;
}
public void setDestination(String destination) {
Destination = destination;
}
public int getSpeedLimit() {
return speedLimit;
}
public void setSpeedLimit(int speedLimit) {
this.speedLimit = speedLimit;
}
public int getDistance() {
return distance;
}
public void setDistance(int distance) {
this.distance = distance;
}
}
and this is the Navigation Class :
package lab;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
/**
* The class Navigation finds the shortest (and/or) path between points on a map
* using the Dijkstra algorithm
*/
public class Navigation {
/**
* Return codes: -1 if the source is not on the map -2 if the destination is
* not on the map -3 if both source and destination points are not on the
* map -4 if no path can be found between source and destination
*/
public static final int SOURCE_NOT_FOUND = -1;
public static final int DESTINATION_NOT_FOUND = -2;
public static final int SOURCE_DESTINATION_NOT_FOUND = -3;
public static final int NO_PATH = -4;
// added variables
private String filename = null; // filename initialization
/**
* The constructor takes a filename as input, it reads that file and fill
* the nodes and edges Lists with corresponding node and edge objects
*
* @param filename
* name of the file containing the input map
*/
public Navigation(String filename) {
ArrayList<VerticesRep> Vertex = new ArrayList<VerticesRep>();
ArrayList<EdgesRep> Edges = new ArrayList<EdgesRep>();
this.filename = filename;
try {
FileReader fr = new FileReader(filename);
BufferedReader in = new BufferedReader(fr);
String line = null;
while ((line = in.readLine()) != null) {
// here i must fill the ArrayLists with the Information
// I need from the text file.
}
in.close();
fr.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
I tried to find out what I actually should use to fill the ArrayList from the text file but the .split won't actually work in my case.
Thank You.