Results 1 to 2 of 2
  1. #1
    nanoo51969 is offline Member
    Join Date
    Mar 2009
    Posts
    1
    Rep Power
    0

    Unhappy I am looking for help with an assignment

    To get started, write a simple program that does the following:
    a. Creates a 99 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 33 block
    b. Creates a 99 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?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    This:
    Java 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.

Similar Threads

  1. stuck on an assignment
    By starchildren3317 in forum New To Java
    Replies: 11
    Last Post: 11-19-2008, 11:03 PM
  2. GUI First Assignment-DUE 8/1/08
    By ljk8950 in forum AWT / Swing
    Replies: 2
    Last Post: 08-01-2008, 04:23 AM
  3. First GUI Assignment
    By ljk8950 in forum New To Java
    Replies: 1
    Last Post: 07-31-2008, 07:29 AM
  4. for Assignment plz help
    By assamhammad in forum New To Java
    Replies: 1
    Last Post: 11-06-2007, 08:35 PM
  5. Help with my assignment java
    By toby in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •