Problem with my double subscript array Java Applet

Status
Not open for further replies.
The program creates a 6x5 table with each column a salesperson and each row a product. 5 products and 4 salespeople (count starts at 0) The last row and last column are the totals.


Code:
import java.awt.*;
import javax.swing.*;

//*************************************************************

public class Test extends JApplet {


    JTextArea outputArea;

    String output;

    int array[][] = new int[5][4];

    int person, product;

//*************************************************************

    public void init() {

	outputArea = new JTextArea();
	Container c = getContentPane();
	c.add( outputArea );

        person = array.length;

        product = array[0].length;

        buildArray ();

	productTotal ();

	personTotal ();

	buildOutput ();

	outputArea.setFont(
		new Font( "Courier", Font.PLAIN, 12 ) );
	outputArea.setText( output );


    }

//*************************************************************
	
    public void buildArray () {
   	
	for ( int i = 0; i < person; i++ ) {
	    
	   for ( int j = 0; j < product; j++ ) {

	       for ( int day = 0; day < 30; day++ )
	       array[j][i] += 1 + (int) ( Math.random() * 5 );
	   }
	   
	}
    }
//*************************************************************
    
    public void productTotal () {

      	for ( int i = 0; i < person; i++ ) {

	    	array[5][i] = 0;
    	
	   for ( int j = 0; j < product; j++ ) {

		array[j][4] = 0;
	   }
	}

	for ( int i = 0; i < person; i++ ) {

	   for ( int j = 0; j < product; j++ ) {

		array[j][4] += array[i][j];
	   }
	}
    }	
//*************************************************************

    public void personTotal () {

	for ( int i = 0; i < product; i++ ) {

	    for ( int j = 0; j < person; j++ ) {

               array[5][j] += array[i][j];
	    }
	}
    }
		
//************************************************************* 
	
    public void buildOutput() {

       output += "       ";

       for ( int i = 0; i < person; i++ )
	   output += "Person" + i + "       ";

       for ( int j = 0; j < product; j++ ) {

	   output += "\nProduct" + j + " ";

	   for ( int i = 0; i < person; i++ )
	       output += array[j][i] + "          ";
       }
    }

}

javac gives no errors but when I run the appletviewer I get applet not initialized plus this in the terminal

java.lang.ArrayIndexOutOfBoundsException
at Test.buildArray(Test.java:53)
at Test.init(Test.java:29)
at sun.applet.AppletPanel.run(AppletPanel.java:341)
at java.lang.Thread.run(Thread.java:536)

What am I doing wrong?
 

Paralis

Ars Scholae Palatinae
930
Looks to me like you've got the variables switched in your loop. You use person to refer to array.length, and then product to refer to array[0].length, but when filling the array, you invert it. Eventually, your program will try to write to array[0][4]; that's what's throwing the exception.

Keep in mind in your other methods that Java numbers array elements starting at zero, so that an array of 5 elements is numbered from 0 - 4, not 1 - 5.
 

TheJet

Ars Praefectus
4,598
Subscriptor++
Also, I think that building the array as "int array[][] = new int[5][4];" and then referencing it as array[5][anything] will give you an array bounds exception, since (as stated above) your valid indices are only 0-4 and 0-3 respectively. (again IIRC, but I may be getting Java mixed up with C here). For a 6x5 array, you really want to declare it that way, i.e. "int array[][] = new int[6][5]" which will give you 0-5 and 0-4 respectively.

TheJet
 
Status
Not open for further replies.