Nikolas Nikolas - 7 months ago 67
Java Question

Sum of columns and rows of 2D array

I've searched up and down for the fix to my issue, but none seem to work. One particular reference-- this and this, and especially this. However, no matter how I implement them, I receive an OutOfBoundsError, which I can't understand.

The program is extra credit for a class. In truth, it is very simple--


Program Description: Use a two dimensional array to solve the following problem. A company has four sales persons (1 to 4) who sell five different products (1 to 5). Once a day, each salesperson passes in a slip for each different type of product sold. Each slip contains:

The sales persons number
The product number
The total dollar value of that product sold that day



Thus, each salesperson passes in between 0 and 5 sales slips per day. Assume that the information from all of the slips for last month is available. Each data line contains 3 numbers (the sales person number, product number, sales).

Write a program that will read all this information for last month’s sales, and summarize the total sales by salesperson by product.


The data provided:

1 2 121.77
1 4 253.66
1 5 184.22
1 1 97.55
2 1 152.44
2 2 104.53
2 4 189.97
2 5 247.88
3 5 235.87
3 4 301.33
3 3 122.15
3 2 301.00
3 1 97.55
4 1 125.66
4 2 315.88
4 4 200.10
4 3 231.45


The error only comes when it tries to calculate the columns. My rows work; no matter how I change the for-loop or any of the indeces in the row or column of the array, it doesn't work. I at first had my rows calculated separately, then my column sums, and it didn't work either. There is something that I'm missing that I'm clearly overlooking.

Here is my code:

import java.io.File;
import java.io.FileNotFoundException;
import java.text.DecimalFormat;
import java.util.Scanner;


public class prog480u {

static Scanner inFile = null;

public static void main(String[] args) {

try {

// create scanner to read file
inFile = new Scanner(new File ("prog480u.dat"));

} catch (FileNotFoundException e) {
System.out.println("File not found!");
System.exit(0);
}

// make the array

int x = 0;
int y = 0;

double[][] profits = new double[4][5];

while (inFile.hasNext()) {

x = inFile.nextInt(); // use sales numbers as coordinates
y = inFile.nextInt();

profits[x - 1][y - 1] = inFile.nextDouble();

}


// check if it's okay


System.out.println("");
double[][] columnProfits = sums(profits);

for (int a = 0; a < columnProfits.length; a++) {
System.out.print((a+1) + "\t");
for (int b = 0; b < columnProfits[a].length; b++) {
System.out.print(columnProfits[a][b] + "\t");
}

System.out.println("");
}

double[] bottomRow = columnSums(columnProfits);

for (int a = 0; a < bottomRow.length; a++) {

System.out.print("Total:" + bottomRow + "\t");

}


}

public static double[][] sums (double[][] q) {

double[][] array = new double[5][6];
array = q;

double sum = 0;

for (int a = 0; a < array.length; a++) {

for (int b = 0; b < array[0].length; b ++) {

sum += array[a][b]; // add everything in the row


}

array[a][4] = sum; // set that row to the last column

sum = 0; // reset sum to 0

}

return array;
}

public static double[] columnSums (double[][]q) {

double[][] array = new double[5][6];
array = q;

double sum2 = 0;

double[] columns = new double [5];

for (int a = 0; a < array.length; a++) {

for (int b = 0; b < array[0].length; b ++) {

sum2 += array[b][a];

columns[b] = sum2;

}

sum2 = 0; // reset sum to 0

}

return columns;
}


}


Thank you very much for your time. I have a feeling my program is close to working, but this small mistake is pushing me over the edge.

Answer

Here's the working code (I cleaned it up a bit):

You were very close, you just needed to swap your max indicies in the for loops. That's why you were getting a java.lang.ArrayIndexOutOfBoundsException

public static double[] columnSums(double[][] q)
{
    double[][] array = q;

    double[] columns = new double[5];

    for (int a = 0; a < array[0].length; a++)
    {
        for (int b = 0; b < array.length; b++)
        {   
            columns[a] += array[b][a];
        }
    }
    return columns;
}