title: Need help fixing java.lang.ArrayIndexOutOfBoundsException
body: <p>I'm trying to implement <a href="http://www.geeksforgeeks.org/dynamic-programming-set-18-word-wrap/" rel="nofollow">this word wrap algorithm</a> in Java. My program takes in the number of paragraphs to wrap, the maximum line length, and the input text. For example:</p>
  
  <pre><code>1
  5
  This is a test.
  </code></pre>
  
  <p>However, after it takes in the input text and runs the algorithm, I get the following runtime error:</p>
  
  <pre><code>Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
  at DynamicProgramming.main(DynamicProgramming.java:74)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:601)
  at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
  </code></pre>
  
  <p>Could it be possible that I have a typo from translating the above code from C++ to Java, or is there kind of error in this logic that would be causing this exception? Thanks.</p>
  
  <pre><code>import java.util.Scanner;
  
  public class DynamicProgramming {
      static int P;
      static int M;
      static String[] inputLine;
  
      public static void printNeatly(int M, int[] inputLineLengths) {
          int n = inputLineLengths.length;
          double[][] extraSpaces = new double[n][n];
          double[][] lineCost = new double[n][n];
          double[] optimalTotalCost = new double[n];
          int[] optimizedLengths = new int[n];
  
          for (int i=1; i &lt;= n; i++) {
              extraSpaces[i][i] = M - inputLineLengths[i-1];
  
              for (int j=i+1; j &lt;= n; j++) {
                  extraSpaces[i][j] = extraSpaces[i][j-1] - inputLineLengths[j-1] -1;
              }
          }
  
          for (int i=1; i&lt;= n; i++) {
              for (int j=i; j &lt;=n; j++) {
                  if (extraSpaces[i][j] &lt; 0) {
                      lineCost[i][j] = Double.POSITIVE_INFINITY;
                  }
                  else if (j == n &amp;&amp; extraSpaces[i][j] &gt;= 0) {
                      lineCost[i][j] = 0;
                  }
                  else {
                      lineCost[i][j] = extraSpaces[i][j]*extraSpaces[i][j];
                  }
              }
          }
  
          optimalTotalCost[0] = 0;
          for (int j=1; j &lt;= n; j++) {
              optimalTotalCost[j] = Double.POSITIVE_INFINITY;
  
              for (int i=0; i &lt;= j; i++) {
                  if (optimalTotalCost[i-1] != Double.POSITIVE_INFINITY &amp;&amp; lineCost[i][j] != Double.POSITIVE_INFINITY &amp;&amp;
                          (optimalTotalCost[i-1] + lineCost[i][j] &lt; optimalTotalCost[j])) {
                      optimalTotalCost[j] = optimalTotalCost[i-1] + lineCost[i][j];
                      optimizedLengths[j] = i;
                  }
              }
          }
      }
  
      public int printOutput(int[] optimizedLengths, int n) {
          int k;
  
          if (optimizedLengths[n] == 1) {
              k = 1;
          }
          else {
              k = printOutput(optimizedLengths, optimizedLengths[n]-1);
          }
  
          System.out.println(optimizedLengths[n]);
          return k;
      }
  
      public static void main(String[] args) {
          Scanner scanner = new Scanner(System.in);
          P = Integer.parseInt(scanner.nextLine());
          int[] inputLineLengths = new int[]{};
  
  
          for (int i=1; i &lt;= P; i++) {
              M = Integer.parseInt(scanner.nextLine());
              inputLine = scanner.nextLine().split("[ ]+");
              inputLineLengths[i] = inputLine.length;
              printNeatly(M, inputLineLengths);
          }
      }
  }
  </code></pre>