Someone please tell me what i did wrong

package trial;

public class Tashi.java {

public static void main(String[] args) {
	int [][] A = {{1,2,3}, {4,5,6}};
	int[][]  B = {{1,2,3}, {4,5,6}};
	int Y[][]  = new int[2][2];
	for(int i = 0; i < Y.length; i++);{
		for(int j = 0; j < Y[i].length; j++);{
for(int k = 0; k < 2; k++)
		 sum = sum + A[2][k] * B[k][2];
	}

}
System.out.print(Y[0][1] + " ");
System.out.println(Y[2][2]);
}

}

Is this from a lesson, or your own homework/practice project?

Where is the variable sum declared? Also Y[2][2] might throw an index out of bounds error. Remember that array indices start at 0, and Y is a 2d array with 2 rows and 2 columns. The largest index you can call is Y[1][1].
Hope this helps :slight_smile:

im practicing for a test

1 Like

i did try using Y[1][1] ITS STILL NOT WORKING

What is the error being thrown?

package trial;

public class Tashi {
	public static void main(String[] args) {
		int [][] A = {{1,2,3}, {4,5,6}};
		int[][]  B = {{1,2,3}, {4,5,6}};
		int sum = 0;
		int Y[][]  = new int[1][1];
		for(int i = 0; i < Y.length; i++);{
			for(int j = 0; j < Y.length; j++);{
			for(int k = 0; k < 6; k++){
		  sum = sum + A[1][k] * B[k][1];
		}
			}
		Y[1][1] = sum;
		sum=0;
		{
			{
				System.out.print(Y[1][0] + " ");
			System.out.println(Y[1][1]);
			}
		}
	  }
		
	}	
	}

i tried out this one again and it did not work the error was;
(Exception in thread β€œmain” java.lang.ArrayIndexOutOfBoundsException: 2
at trial.Tashi.main(Tashi.java:12))

The error is coming from k. The arrays in A and B have lengths of 3, and k can go up to 5 (based on β€œk<6” in the for loop).

Also you decreased the size of Y. The largest value you can call now is Y[0][0]. Remember the arrays start at index 0, so if you want to have values at Y[1][1], you need to have an array of size [2][2].

okay got it thank you!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.