# I am looking for help with an assignment

• 03-23-2009, 09:48 PM
nanoo51969
I am looking for help with an assignment
To get started, write a simple program that does the following:
a. Creates a 9×9 array of integers and
i. Fills each slot of the array with a random integer from {0, 1, … , 9}
ii. Displays the contents of the array
iii. Displays the integers in each row, column, and 3×3 block
b. Creates a 9×9 array of java.util.Set objects that hold Integers and
iv. Fills each set with some integers from {1, 2, … , 9}
v. Displays the contents of each set (hint: use the toString method of the Set class)

Here is what I have so far:

package randomboard;

public class Main
{
public static void main(String[] args)
{
//create the grid
final int row = 9;
final int col = 9;
final int box = 9;
int [][] board = new int [row][col];
//fill the grid
for (int i=0; i<board.length; i++)
board[i][board[i].length] = (int)(Math.random()*9);

//display output
for(int i=0; i<board.length; i++)
{
for(int j=0; j<board[i].length; j++)
System.out.print(board[i][j]+"");
System.out.println();
}
}
}
When I run this I get an error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9

I am so lost, can anyone help?
• 03-23-2009, 10:41 PM
Fubarable
This:
Code:

`board[i][board[i].length] = (int)(Math.random()*9);`
will have i loop from 0 to 8, that's fine, but the second index for your array here will always be 9, the length of board[i], and that's not fine since arrays go from 0 to length - 1. I think that you want to have two nested for loops here with i going from 0 to length and j going from 0 to length with you filling board[i][j] with random numbers inside the loop.