this is my code, can anyone find out why do i get error?
i don get error if i change my array column to lower than row.

import javax.swing.JOptionPane;
public class Chern_sales{
	public static void main(String[] args){
		
		double[][] sales = new double[6][7];
		double[] total = new double[6];
		double[] totalPerDay = new double[6];
		int OFF_DAY = 0;
		
		for(int i = 0; i<sales.length; i++){
			for(int j = 0; j<sales[i].length; j++){
			sales[i][j] = Double.parseDouble(JOptionPane.showInputDialog(null,"Inputing for..\nSalesperson "+(i+1)+"\nDay "+(j+1)+" Sales.") );
			
			if(sales[i][j] == -1){
				++OFF_DAY;
			}
			else {
				//total for each sales person
				total[i] += sales[i][j];
				//total for each day
				totalPerDay[j] += sales[i][j];
				}
			}
			// initialize offday to zero again.
			OFF_DAY = 0;
		}
		
		System.out.print("\t\t");
		for(int j = 0; j<sales[0].length; j++){
			System.out.print("Day "+j+"\t");
		}
		System.out.println("Total per week\n");
		
		for(int i = 0; i<sales.length; i++){
			System.out.print("Salesperson "+(i+1)+"\t");
			for(int j = 0; j<sales[i].length; j++){
			if(sales[i][j] != -1){
				System.out.print("RM"+sales[i][j]+"\t");
			}
			else
				System.out.print("OFF\t");
			}
			System.out.println(total[i]);
		}
		
		System.out.print("Total per day \t");
		for(int i = 0; i<sales[0].length; i++){
			System.out.print("RM"+totalPerDay[i]+"\t");
		}
		System.out.println("");

		System.exit(0);
	}
}
Comments
error is there
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6 at Chern_sales.main(Chern_sales.java:21)

error occurs after the seventh input

this

totalPerDay[j] += sales[i][j];

should not that first j be i?

Hint: Yes it should, according to the way that array is declared, but maybe (guaranteed) you declared the array wrong, in which case it shouldn't be.

Edited 6 Years Ago by masijade: n/a

Think about how you're accessing your array. You're going from index 0 to index sales.length in one for loop. In the other for loop, you're going from index 0 to index sales.length. The problem is that you're using these indexes to access the array

totalPerDay[j] += sales[j];

Which is causing it to go out of bounds. In order to help you solve your problem I need to know what you are actually trying to do, because the way you index the array depends on what you want to do with it. And you're doing it wrong, otherwise you wouldn't be going out of bounds, which means you are stepping past the maximum index that you have set aside for your array.

edit: sorry, masijade got to it while I was writing my post apparently.

Edited 6 Years Ago by BestJewSinceJC: n/a

if
totalPerDay += sales[j];
the total is total of the 6 days only rite?
i wan the total sales of each day(7 days) by all salesperson

Edited 6 Years Ago by chern4ever: n/a

if
totalPerDay += sales[j];
the total is total of the 6 days only rite?
i wan the total sales of each day(7 days) by all salesperson

Read the hint. Where did you declare the totalPerDay array, and with what length?

No problem. Check the JCreator preferences/settings/options for a possible setting to "keep the execution shell open" or something to that effect. I do not know JCreator myself, but I assume it is opeing a command shell to execute in, which means that as soon as the execution is finished the shell will close. So, once again, simply search the options.

This article has been dead for over six months. Start a new discussion instead.