im trying to save elements from an input file which contains lines such as:
numNodes 2
numEdges 2
node 0 0.62
node 1 0.83
edge 0 1 -silence- 0 78
edge 0 2 -silence- 0 38
I've opened the file using scanner and broke the input into lines and words, to access each individual string.
Im trying to save each double in a node line within an array containing doubles:
this.nodeTimes = new double[numNodes];
And likewise save the three words on each edge line in an adjacency matrix:
this.adjMatrix = new Edge[numNodes][numNodes];
However in my attempts I hit ArrayIndexOutOfBoundsExceptions
for (int i = 0; i < list.size(); i++) {
String line = list.get(i);
...
} else if (words[0].equals("node")) {
nodeTimes[numNodes] = Double.parseDouble(words[3]); // trying to get doubles
} else if (words[0].equals("edge")) {
adjMatrix[Integer.parseInt(words[1])][Integer.parseInt(words[2])] = new Edge((words[3]), Integer.parseInt(words[4]), Integer.parseInt(words[5])); // trying to store edge values
this similarly happens if I try something like:
while(openedFile.hasNextDouble()){
nodeTimes[numNodes] = openedFile.nextDouble();
}
Not quite sure how im getting hung up
Edit:
accessing with:
nodeTimes[Integer.parseInt(words[1])] = Double.parseDouble(words[3]);
and
adjMatrix[Integer.parseInt(words[1])][Integer.parseInt(words[2])] = new Edge((words[3]), Integer.parseInt(words[4]), Integer.parseInt(words[5]));
also seem to give me an exception.
Looking at your code again, I see where the issue is.
You are creating an array of size numNodes with this line.
this.nodeTimes = new double[numNodes];
Then you are trying to access the index at numNodes with this line
nodeTimes[numNodes] = Double.parseDouble(words[3]);
Accessing the index at the size of the array will always give you an ArrayIndexOutOfBoundsExceptions
You can only go from 0 to numNodes - 1