Reputation: 205
I'm trying to implement this word wrap algorithm in Java. My program takes in the number of paragraphs to wrap, the maximum line length, and the input text. For example:
1
5
This is a test.
However, after it takes in the input text and runs the algorithm, I get the following runtime error:
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)
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.
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 <= n; i++) {
extraSpaces[i][i] = M - inputLineLengths[i-1];
for (int j=i+1; j <= n; j++) {
extraSpaces[i][j] = extraSpaces[i][j-1] - inputLineLengths[j-1] -1;
}
}
for (int i=1; i<= n; i++) {
for (int j=i; j <=n; j++) {
if (extraSpaces[i][j] < 0) {
lineCost[i][j] = Double.POSITIVE_INFINITY;
}
else if (j == n && extraSpaces[i][j] >= 0) {
lineCost[i][j] = 0;
}
else {
lineCost[i][j] = extraSpaces[i][j]*extraSpaces[i][j];
}
}
}
optimalTotalCost[0] = 0;
for (int j=1; j <= n; j++) {
optimalTotalCost[j] = Double.POSITIVE_INFINITY;
for (int i=0; i <= j; i++) {
if (optimalTotalCost[i-1] != Double.POSITIVE_INFINITY && lineCost[i][j] != Double.POSITIVE_INFINITY &&
(optimalTotalCost[i-1] + lineCost[i][j] < 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 <= P; i++) {
M = Integer.parseInt(scanner.nextLine());
inputLine = scanner.nextLine().split("[ ]+");
inputLineLengths[i] = inputLine.length;
printNeatly(M, inputLineLengths);
}
}
}
Upvotes: 1
Views: 1118
Reputation: 6437
Just looking at the printNeatly
method:
You get the length of the array like this:
int n = inputLineLengths.length;
But in your loops, you go up to and including n
:
for (int i=1; i <= n; i++) {
You need to stop just before, because arrays (in Java) are indexed from 0
to n-1
(so it's also very likely that you mean to start your arrays from 0
, and not 1
)
for (int i=1; i < n; i++) {
Upvotes: 1
Reputation: 178263
First, your inputLineLengths
array is initialized to a length of 0
, so you can't put anything into the array.
Initialize it to a length of P
first.
int[] inputLineLengths = new int[P];
Second, your for
loop needs to range over valid index values, which are from 0
to array's length - 1
. Change your for
loop to:
for (int i = 0; i < P; i++) {
You have other for
loops outside of main
that need to be changed similarly.
Upvotes: 0