For this task I’m supposed to create a “wannabe” network that is supposed to be a graph. The first file you read contains a textfile with vertices from/to and some other data. I have also an inner counter who counts how many times addVertex has been used. So far it’s correct and the test print is correct, but when I run it has no vertices in the list even tough it says it has been added.
Any ideas why id won’t add to it’s list and any ideas?
Here is how I read:
static Graph graph;
private static void createNetwork(String fil1) {
try {
Scanner sc = new Scanner(new File(fil1));
graph = new Graph();
while (sc.hasNextLine()) {
String line = sc.nextLine();
String[] split = line.split("\t");
int[] connections = new int[split.length];
// System.out.println(line); // test utskrift
for (int i = 0; i < split.length; i++) {
connections[i] = Integer.parseInt(split[i].trim());
}
graph.addVertex(connections);
}
} catch (Exception e) {
}
}
And some other methods that is being called on:
public void addVertex(int[] cons) {//, int tid, int ore) {
if (cons == null) {
return;
}
boolean added = false;
Vertex fra, til;
int tid = cons[2];
int ore = cons[3];
fra = new Vertex(cons[0], cons[1], cons[2], cons[3]);
til = new Vertex(cons[1], cons[0], cons[2], cons[3]);
if (verticies.contains(fra) == false) { //, tid, ore)
System.out.println(
fra.id + " --> " + til.id + " Ble lagt til i lista! " + size);
size++;
added = verticies.add(fra); //, tid, ore
// addEdge(fra, til, tid, ore);
// addEdge(til, fra, tid, ore);
// addBiEdges(fra, til, tid, ore);
// return true;
}
}
public boolean addBiEdges(Vertex fra, Vertex til, int tid, int ore) throws IllegalArgumentException {
return false; // addEdge(fra, til, tid, ore) && addEdge(til, fra, tid, ore);
}
public void addEdge(Vertex fra, Vertex til, int tid, int ore) throws IllegalArgumentException {
if (verticies.contains(fra) == false)
throw new IllegalArgumentException(fra.id + " er ikke med i grafen!");
if (verticies.contains(til) == false)
throw new IllegalArgumentException(til.id + " er ikke med i grafen!");
Edge e = new Edge(fra, til, tid, ore);
if (fra.findEdge(til) != null) {
return;
} else {
fra.addEdges(e);
til.addEdges(e);
edges.add(e);
// return true;
}
}
class Graph {
public static int size;
HashMap<Integer, Vertex> graph;
protected List<Vertex> verticies;
protected List<Edge> edges;
// Brukes til Dijkstras algoritmen
public List<Vertex> kjent;
public List<Vertex> ukjent;
public Graph() {
graph = new HashMap<Integer, Vertex>();
kjent = new ArrayList<Vertex>();
ukjent = new ArrayList<Vertex>();
verticies = new ArrayList<Vertex>();
edges = new ArrayList<Edge>();
}
}
They aren’t added to the list in the first place.
addVertex()prints out the message about the vertex being added to the list, although it hasn’t done so, yet. Then it tries to but fails, resulting in an exception being thrown byArrayList.add()The exception gets caught increateNetwork(), so you won’t notice something went wrong.Don’t catch exceptions you won’t handle. Don’t log actions before they have been carried out.