-1

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]);
}
Curiosa Globunznik
  • 3,129
  • 1
  • 16
  • 24
  • what's line 17 exactly, can't locate it in your sample? I suppose, but am not sure: `array[i] = Integer.parseInt(str[2]) - Integer.parseInt(str[1]);` If so: use a [debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems), set a break point there and have a look at the values of `str[]` and `s`, it should make it clear, what's going on. – Curiosa Globunznik Nov 05 '20 at 10:41
  • 1
    Does this answer your question? [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Curiosa Globunznik Nov 05 '20 at 10:47

2 Answers2

0

You get a clue from the exception message,

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 1 at Main.main(Main.java:17)

Since the code was formatted earlier, am assuming line 17 is array[i] = Integer.parseInt(str[2]) - Integer.parseInt(str[1]);. In which case str[2] has Index 2 and is out of bounds. str is probably length 1 depending on the input you gave. Arrays in Java start with 0index. So you probably should use array[i] = Integer.parseInt(str[1]) - Integer.parseInt(str[0]); instead.

MoonBrew
  • 1
  • 1
0

Add check before getting the values from the str array as below -

  if (str.size() > 2){
    array[i] = Integer.parseInt(str[2]) - Integer.parseInt(str[1]);
        if (array[i] < 0) {
            array[i] *= (-1);
        }
        i++;
  }
Priyanka
  • 1,791
  • 1
  • 7
  • 12