I'm trying to calculate the money times the percent (i.e. $1000*.04 = $40) and print out the result (i.e. $40). I have a 2D String array holding the information. First off I want to print the percent for each row, depending on certain factors that are shown in the first for loop. Then I'd like to display the result of the money time the percent in the second for loop. I have the 1D Double array holding the percent shown as a decimal that will be multiplied to the money to produce the result. I keep getting this error --> java.lang.ArrayIndexOutOfBoundsException: 3
So I figure it's something wrong with the 1D Double array, but I don't know what.
FileReader fr = new FileReader("input.txt");
BufferedReader br = new BufferedReader(fr);
//2D array
String[][] input = new String[15][4];
//1D array
Doubt[] in = new Double[3];
for(int row=0; row<input.length; row++){
int a = Integer.parseInt(input[row][1]);
int b = Integer.parseInt(input[row][2]);
double s = Double.parseDouble(input[row][3]);
if(a>=50 && b==1){
System.out.println(input[row][0] + "\t\t2%");
in[row]=0.02;
}
if(a>=25 && b==1){
System.out.println(input[row][0] + "\t\t4%");
in[row]=0.04;
}
if((a>=15 && b==1){
System.out.println(input[row][0] + "\t\t6%");
in[row]=0.06;
}
}
for(int row=0; row<input.length; row++){
int a = Integer.parseInt(input[row][1]);
int b = Integer.parseInt(input[row][2]);
double s = Double.parseDouble(input[row][3]);
System.out.println(input[row][0] + "\t\t" + s*in[row]);
}
This might be because your in[]
array is of size 3. but for row=3
this in[row]=0.06;
statement will throw java.lang.ArrayIndexOutOfBoundsException
.
So declare in[]
as Double[] in = new Double[input.length];