Click to See Complete Forum and Search --> : a simple program


April 30th, 2000, 05:51 PM
Hi all! I am trying to create a table of values with the last row blank so that when the user clicks at the button('sum' or 'average') it will display the result in the last row.
whats wrong with this?it says i cant assign values to the array in the array declaration..??



import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;

public class array extends Applet implements ActionListener
{
private Button sum, average;
int total=0;
int[][] a=new int[4][4];

//int []a;
//int []b;
//int[] a= new int[4];
//int[] b= new int[4];
//int[] c= new int[4];
//total = sum(a);
a[0][0]=1;
a[1][0]=2;
a[2][0]=3;
a[3][0]=0;
a[1][1]=4;
a[2][1]=5;
a[3][1]=6;
a[4][1]=0;
a[0][2]=7;
a[1][2]=8;
a[2][2]=9;
a[3][2]=0;
a[0][3]=10;
a[1][3]=11;
a[2][3]=12;
a[3][3]=0;
//a={1,2,3,0};
//a[0]=1;
//a[1]=1;
//a[2]=1;
//a[3]=0;
//b={5,6,7,0};
//b[0]=1;
//b[1]=1;
//b[2]=1;
//b[3]=0;
//Button sum;
//Button average;

sum= new Button("Sum"); //supplying the 2 buttons
add(sum);
sum.addActionListener(this);

average= new Button("Average");
add(average);
sum.addActionListener(this);

for (int i = 0; i < a.length; i++){
System.out.println( a[i] );
}
public void paint (Graphics g){
public int sum(int[][] a){
total=0;
for(int row=0;row<4;row++){ //to get the sum of the row
for(int col=0;col<4;col++){
total=total+a[row][col];
return total;
//g.drawString("total: " +total,a[3][3]
}
}
}

public int average(int[][] a){
for (int row=0;row<4;row++){
for(int col=0;col<4;col++){
int total=total+a[row][col];
int average (total/3);
return average;
}
}
}




}

dogbear
May 2nd, 2000, 05:52 AM
WhoeverYouAre,

You are probably the same person who asked about the String double array for French words, yes?

You are making the same mistakes as before:
You cannot assign values to arrays outside a method.
So, the trick is to declare the array outside any methods(so that they will be global), and then assign values for them in any method(usually the init() method).

Regards,
dogBear