I searched for a fix and fix for my problem, but none of them work. One specific link is this and this , and especially this . However, no matter how I implement them, I get an OutOfBoundsError that I cannot understand.
The program is an additional loan for the class. In truth, itβs very simple -
Program Description: Use a two-dimensional array to solve the following problem. The company has four sellers (1 to 4) who sell five different products (1 to 5). Once a day, each seller skips passes for each sold product of a different type. Each slip contains:
Number of sellers
Product number
The total dollar value of this product sold on that day
Thus, each seller passes from 0 to 5 sheets of trade per day. Suppose that information from all misses for the last month is available. Each data line contains 3 numbers (seller number, product number, sales).
Write a program that will read all this information over the last months of sales, and summarizes the total sales volume of the seller by product.
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 occurs when trying to calculate columns. My ranks are working; no matter how I modify the for loop or any of the indices in the row or column of the array, this does not work. At first I calculated my rows separately, then my sum of columns, and it didn't work either. There is something that I am missing, which I clearly do not notice.
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 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; } }
Thanks so much for your time. I have a feeling that my program is close to work, but this small mistake pushes me to the edge.