PDA

View Full Version : Please can someone tell me why compiler reports index out of bounds


clharri23
12-10-2009, 06:38 AM
I'm doing a problem where i have to multiply two matrices using two arrays:

monthlySales[4][3]*prices[3][4];

and save the results in the following array:

totalMonthlyProfit[4][4] += monthlySales[4][3]*prices[3][4];

using for loops. However, the compiler is reporting index out of bounds at the following locations in the source code, but i'm not sure why. I think it may have to do with the way I call methods at the highlighted locations in the code. I provide the code and the problem areas in bold below. Thank you for any help you can provide:


/*
* This program uses a two dimensional array to store and manipulate data that represents statistics of a computer supply store
* The program uses 9 methods to accomplish this 1) determines total price 2) component that sold the most in a particular month 3) highest dollar selling component 4) best selling model 5) total number of specific components sold
* 6) total dollar amount of specific component sold 7) total number of units sold in a particular month 8) total dollar amount sold for all components and models over a particular month 9) returns a 2-d array that holds the number of units sold over a two month period.
*/
import java.util.Scanner;
public class CompSupplies
{
// ***COULD STAGGER ALL COLUMS AND ROWS SO THAT THE TABLE BORDERS BARE HEADERS ([1][1], [1][2], ETC.)***

public static double[][] getPricingMatrix(int[][] monthlySales, double[][] prices) //1st method
{
double[][] totalMonthlyProfit = new double [4][4];

for(int i = 0, j = 0; i < 3 && j < 3; i++, j++)
{
for(int k = 0; k < 2; k++)
{
totalMonthlyProfit[i][j] += monthlySales[i][k]*prices[k][j]; //java.lang.ArrayIndexOutOfBoundsException: 3

}
}
return totalMonthlyProfit;
}

public static int bestSellingComponent(int [][] monthlySales)
{
int max, index = 0;
for(int i = 0; i < monthlySales.length; i++)
for(int j = 0; j < 2; j++)
{
max = monthlySales[0][0];
if(max > monthlySales[i][j])
{
max = monthlySales[i][j];
index = i;
}

}

return index;

}

public static int highestDollarAmountComponent(int [][] monthlySales, double [] [] prices)
{
double [] [] highestDollarAmount = getPricingMatrix(monthlySales, prices);
double maximum = 0; int index = 0;

for(int i = 0; i < highestDollarAmount.length; i++)
for(int j = 0; j < 2; j++)
{
maximum = highestDollarAmount[0][0];

if(maximum > highestDollarAmount[i][j])
{
maximum = highestDollarAmount[i][j];
index = i;
}

}

return index;

}

public static int bestSellingModel(int[][] monthlySales, int component) //4th method
{
int bestSeller = 0;
int i = component;

for(int j = 0; j <= 2; j++)
{
if(monthlySales[i][j] > bestSeller)
{
bestSeller = j;
}
}
return bestSeller;
}

public static int unitsSoldByComponent(int[][] monthlySales, int component) //5th method
{
int total = 0;

for(int i = component, j = 0; j <= 2; j++)
{
total += monthlySales[i][j];
}
return total;
}

double dollarAmountSoldByComponent(int[][] monthlySales, double [][] prices, int component)
{
double [][] totalDollarAmount = getPricingMatrix(monthlySales, prices);

if(component == 0)
return totalDollarAmount[component][component];
else if(component == 1)
return totalDollarAmount[component][component];
else if(component == 2)
return totalDollarAmount[component][component];
else
return totalDollarAmount[component][component];



}

public static int totalUnitsSold(int [] [] monthlySales)
{
int sum = 0;
for(int i = 0; i < monthlySales.length; i++)
{
for(int j = 0; j < 2; j++)
{
sum += monthlySales[i][j];
}

}

return sum;

}



public static double totalDollarAmountSold(int[][] monthlySales, double[][] prices) //8th method: May need to change parameters back to int[][] monthlySales, double[][] prices)
{
double total = 0.0; //java.lang.ArrayIndexOutOfBoundsException: 3

double[][] totalMonthlyProfit = getPricingMatrix(monthlySales, prices);

for(int i = 0; i < totalMonthlyProfit.length; i++)
{
for(int j = 0; j < 2; j++)
{
total += totalMonthlyProfit[i][j];
}
}
return total;
}

public static int[][] combineMonthlySales(int[][] monthlySales1, int[][] monthlySales2) //9th method
{
int[][] totalSold = new int[4][3];

for(int i = 0; i < monthlySales1.length; i++)
{
for(int j = 0; j < 2; j++)
{
totalSold[i][j] = (monthlySales1[i][j] + monthlySales2[i][j]);
}
}
return totalSold;
}


public static void main (String [] args)
{
final int SENTINEL = -1;
int component = 0;
double [] [] prices = new double [3][4]; //for both months

//Must do the same for the other month and price arrays as well:

//October monthlySales table
int[][] octoberSales = new int [4][3]; //total quantity sold in October
int[][] novemberSales = new int[4][3]; //total quantity sold in November
//Call on 1st method for matrix product 2-d array (double[][] totalProfit)
double[][] octoberProfitTable = new double[4][4]; double[][] novemberProfitTable = new double [4][4];
double octoberProfit; double novemberProfit;

Scanner input = new Scanner(System.in);

prices[0][0] = 259.99; prices[0][1] = 49.99; prices[0][2] = 199.99; prices[0][3] = 369.99;
prices[1][0] = 288.99; prices[1][1] = 81.99; prices[1][2] = 89.99; prices[1][3] = 199.99;
prices[2][0] = 196.99; prices[2][1] = 139.99; prices[2][2] = 419.99; prices[2][3] = 419.99;

//Remeber to place this array table with november sales and novemberPrices
octoberSales[0][0] = 448; octoberSales[0][1] = 788; octoberSales[0][2] = 354;
octoberSales[1][0] = 458; octoberSales[1][1] = 581; octoberSales[1][2] = 314;
octoberSales[2][0] = 745; octoberSales[2][1] = 821; octoberSales[2][2] = 232;
octoberSales[3][0] = 299; octoberSales[3][1] = 400; octoberSales[3][2] = 275;

octoberProfit = totalDollarAmountSold(octoberSales, prices); //calling 8th method //java.lang.ArrayIndexOutOfBoundsException: 3


//Printing out table for october profits:
octoberProfitTable = getPricingMatrix(octoberSales, prices);
for(int i = 0; i < octoberProfitTable.length; i++)
{
System.out.println();
for(int j = 0; j < 2; j++)
{
System.out.print(octoberProfitTable[i][j] + "\t");
}
}

//November info (novemberSales table, novemberProfit (matrix product table) novemberProfitDollarAmount):
novemberSales[0][0] = 293; novemberSales[0][1] = 647; novemberSales[0][2] = 454;
novemberSales[1][0] = 0; novemberSales[1][1] = 500; novemberSales[1][2] = 424;
novemberSales[2][0] = 720; novemberSales[2][1] = 794; novemberSales[2][2] = 262;
novemberSales[3][0] = 193; novemberSales[3][1] = 364; novemberSales[3][2] = 43;

novemberProfit = totalDollarAmountSold(novemberSales, prices); //java.lang.ArrayIndexOutOfBoundsException: 3


//Printing out table for november profits:
novemberProfitTable = getPricingMatrix(novemberSales, prices);
for(int i = 0; i < novemberProfitTable.length; i++)
{
System.out.println();
for(int j = 0; j < 2; j++)
{
System.out.print(novemberProfitTable[i][j] + "\t");
}
}

System.out.println("\t\t\tOctober and November Stats:");
System.out.print("0 = Processors, 1 = Memory, 2 = HDD, 3 = Video Card.\nEnter the corresponding component number to view the best selling component model, or enter " + SENTINEL + " to quit: ");
component = input.nextInt();

while(component != SENTINEL) //method 4 call
{
if(component == 0)
{
if (bestSellingModel(octoberSales, component) == 0)
System.out.println("The Intel Core 2 Quad Q9550 processor model sold the most. " );
else if(bestSellingModel(octoberSales, component) == 1)
System.out.println("The Intel Core i7 920 processor model sold the most. " );
else if(bestSellingModel(octoberSales, component) == 2);
System.out.println("The AMD Phenom II X4 965 processor model sold the most. " );
}
else if(component == 1)
{
if(bestSellingModel(octoberSales, component) == 0)
System.out.println("The Crucial DDR2-800, 2x 1GB memory model sold the most. " );
else if(bestSellingModel(octoberSales, component) == 1)
System.out.println("The Mushkin DDR3-1333, 3x 1GB memory model sold the most. " );
else if(bestSellingModel(octoberSales, component) == 2)
System.out.println("The OCZ DDR3-1600, 3x 2GB memory model sold the most. " );
}

else if(component == 2)
{
if(bestSellingModel(octoberSales, component) == 0)
System.out.println("The Western Digital VelociRaptor, 300GB HDD model sold the most. " );
else if(bestSellingModel(octoberSales, component) == 1)
System.out.println("The Seagate Barracuda 7200.12, 1TB HDD model sold the most. " );
else if(bestSellingModel(octoberSales, component) == 2)
System.out.println("The Intel X25-M G2, 160GB HDD model sold the most. " );
}

else if(component == 3)
{
if(bestSellingModel(octoberSales,component) == 0)
System.out.println("The eVGA nVIDIA GeForce GTX 285, 1GB Video Card model sold the most. " );
else if(bestSellingModel(octoberSales, component) == 1)
System.out.println("The XFX ATI Radeon HD4890, 1GB Video Card model sold the most. " );
else if(bestSellingModel(octoberSales, component) == 2)
System.out.println("The Sapphire ATI Radeon HD5870, 1GB Video Card model sold the most. " );
}
System.out.print("Enter another component to view best selling model, or enter " + SENTINEL + " to quit: ");
component = input.nextInt();
}

//method 5 call
System.out.print("Enter desired component number to view total number of units sold for that component (all models), or enter " + SENTINEL + " to quit: ");
component = input.nextInt();

while(component != SENTINEL)
{
System.out.println("total number of units sold for component " + component + " is " + unitsSoldByComponent(octoberSales, component) + ".");
System.out.print("Enter another component number, or enter " + SENTINEL + " to quit: ");
component = input.nextInt();
}

//method 8 call:
System.out.println("Total dollar amount sold for all components and models in October: $" + octoberProfit + ".\nTotal dollar amount sold for all components and models in November: $" + novemberProfit + ".");

//method 9 call:
System.out.println("Number of units sold in October and November combined:");
for(int i = 0; i < combineMonthlySales(octoberSales, novemberSales).length; i++)
{
System.out.println();
for(int j = 0; j < 2; j++)
{
System.out.print(combineMonthlySales(octoberSales, novemberSales)[i][j] + "\t");
}
}




} //end main
} //end class