Search code examples
javaarrayschar2dindexoutofboundsexception

Out of bounds exception despite loops being in range of array length


So the program is supposed to make an odd sized array between the sizes of 3 and 11 from user input and then fill that board with a character at certain places to get patterns. Everything was going fine until I tried returning the array which gave me 2 out of bounds exceptions even though I set my loops to be less than the dimensions. I used 5 as an example here to try and get a 5 by 5 array. Here is the main.

public static void main (String [] args) {

    int dimension = findDimension();
    char [] [] array2d = new char [dimension] [dimension];

    char star = '*';

    array2d = leftDiagonal(star, dimension); // Get out of bounds here
    print(array2d); 
} 

The method that asks for user input "findDimension()"

public static int findDimension() {
    int dimension = 0;
    Scanner keybd = new Scanner(System.in); 
    do {
        System.out.print("Enter an odd integer between 3 and 11 please: ");
        dimension = keybd.nextInt();
    } while (dimension%2 == 0);
    return dimension;            // Everything seems fine here, no errors
}

Method that prints the array

public static void print(char [] [] arrayParam) {
    System.out.println("-----------");
    System.out.println(arrayParam);
    System.out.println("-----------");
}

Method that sets the pattern "leftDiagonal"

public static char [] [] leftDiagonal(char starParam, int dimenParam) {
    char [] [] leftD = new char [dimenParam] [dimenParam];
    for (int i = 0; i < dimenParam; i++){ 
        for (int j = 0; i < dimenParam; j++) {
            leftD [i][j] = starParam;  // Gets error here
        }
    }
    return leftD;
}

The output should be

-----------                             
 * * * * *
 * * * * *
 * * * * *
 * * * * *
 * * * * *
----------- 

Well technically it should be

 -----------                             
  *    
    *   
      *  
        * 
          *
 -----------  

but at the moment I just want to get any output. I was originally planning to fill all the spaces with blank spaces ' ' and then fill the ones I need in with characters but I can't even get the array to print out first. Thank you for anyone willing to help.


Solution

  • An error occurs because of the inner loop condition.

    public static char[][] leftDiagonal(char starParam, int dimenParam) {
        char[][] leftD = new char[dimenParam][dimenParam];
        for (int i = 0; i < dimenParam; i++) {
            for (int j = 0; j < dimenParam; j++) { // i -> j
                leftD[i][j] = starParam;  // Gets error here
            }
        }
        return leftD;
    }
    

    There are many ways to solve the problem. You can just print the array without initializing it.

    public static char[][] leftDiagonal(char starParam, int dimenParam) {
        char[][] leftD = new char[dimenParam][dimenParam];
        for (int i = 0; i < dimenParam; i++) {
            for (int j = 0; j < dimenParam; j++) {
                if(i==j) {
                    System.out.print(starParam);
                } else {
                    System.out.print("  ");
                }
            }
            System.out.println();
        }
        return leftD;
    }