3

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.

  • Decide if the answer is helpful, and then... Accept it! ```If you want to say "thank you," vote on or accept that person's answer, or simply pay it forward by providing a great answer to someone else's question.``` – Madplay Apr 18 '19 at 06:31

1 Answers1

2

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;
}
Madplay
  • 1,027
  • 1
  • 13
  • 25
  • 1
    Thank you, I feel silly for not noticing that sooner. I get something printed out now but I get the memory address instead of any of the expected output. I set my array "array2d" equal to my leftDiagonal method so why does this happen? Do I need a loop to print out the contents? – Confused Student Apr 18 '19 at 03:08
  • 1
    Edited my answer. ```Do I need a loop to print out the contents?``` That's correct! – Madplay Apr 18 '19 at 03:11
  • Oh sorry, I was inpatient again. It's a bad habit of mine, thank you for the help. – Confused Student Apr 18 '19 at 03:15
  • @ConfusedStudent Yes. It was nice talking to you. Have a nice day! :D – Madplay Apr 18 '19 at 03:22