I am trying to find out shortest path using Dijkstra's algorithm. but my code is showing "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 1 at Main.main(Main.java:17)
public static void main(String args[]) throws Exception {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter input file name: ");
String file = scanner.nextLine();
BufferedReader br = new BufferedReader(new FileReader(file));
String s;
int array[] = new int[120];
int count = 0;
int i = 0;
while ((s = br.readLine()) != null) {
if (count > 0 && count < 121) {
String str[] = s.split(" ");
array[i] = Integer.parseInt(str[2]) - Integer.parseInt(str[1]);
if (array[i] < 0) {
array[i] *= (-1);
}
i++;
}
count++;
}
int temp;
for (int j = 0; j < array.length; j++) {
System.out.println(array[j]);
}
for (int j = 0; j < array.length; j++) {
System.out.println(array[j]);
for (int k = j + 1; k < array.length; k++) {
if (array[j] > array[k]) {
temp = array[j];
array[j] = array[k];
array[k] = temp;
}
}
}
System.out.println("Shortest path: " + array[0]);
System.out.println("Second Shortest path: " + array[1]);
}