title: ArrayIndexOutOfBoundsException in Dijkstra's shortest path algorithm
body: <p>I am trying to find out shortest path using Dijkstra's algorithm. but my code is showing &quot;Exception in thread &quot;main&quot; java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 1         at Main.main(Main.java:17)</p>
  <pre><code>public static void main(String args[]) throws Exception {
      Scanner scanner = new Scanner(System.in);
      System.out.print(&quot;Enter input file name: &quot;);
      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 &gt; 0 &amp;&amp; count &lt; 121) {
              String str[] = s.split(&quot; &quot;);
              array[i] = Integer.parseInt(str[2]) - Integer.parseInt(str[1]);
              if (array[i] &lt; 0) {
                  array[i] *= (-1);
              }
              i++;
          }
          count++;
      }
      int temp;
      for (int j = 0; j &lt; array.length; j++) {
          System.out.println(array[j]);
      }
      for (int j = 0; j &lt; array.length; j++) {
          System.out.println(array[j]);
          for (int k = j + 1; k &lt; array.length; k++) {
              if (array[j] &gt; array[k]) {
                  temp = array[j];
                  array[j] = array[k];
                  array[k] = temp;
              }
          }
      }
      System.out.println(&quot;Shortest path: &quot; + array[0]);
      System.out.println(&quot;Second Shortest path: &quot; + array[1]);
  }
  </code></pre>