Trying to make a function that returns a 2D array, with using switch inside the function. Getting the "Duplicate local variable <board>" warning

Viewed 46

I'm writing a simple program that displays a 2D array. User will be given 4 options to choose from, but for simplicity sakes of this question, I rewrote the program so there's only 2 option enclosed in the switch, and that the second option will always be chosen. The code does not print anything, it only prints "This is your array :". I'm seeing "Duplicate local variable board" in my errors page but I do not know how to fix this.

Here is my code:

public class Apples {
    static char[][] createBoard(int boardType) {
        char[][] board = new char[8][10];
        switch (boardType) {
            case 1:
                char board[][] = {
                        {' ', '1', '2', '3', '4', '5', '6', '7', '8', '9'},
                        {'1', '#', '#', '#', '-', '@', '-', '#', '#', '#'},
                        {'2', '#', '#', '-', '@', '@', '@', '-', '#', '#'},
                        {'3', '#', '-', '@', '@', '-', '@', '@', '-', '#'},
                        {'4', '-', '@', '@', '@', '@', '@', '@', '-'}};
                break;
            case 2:
                char board[][] = {
                        {' ', '1', '2', '3', '4', '5'},
                        {'1', '-', '-', '-', '-', '-'},
                        {'2', '-', '@', '@', '@', '-'},
                        {'3', '-', '-', '@', '-', '-'},
                        {'4', '-', '-', '@', '-', '-'},
                        {'5', '-', '-', '-', '-', '-'}};
                break;
        }
        return board;
    }

    public static void main(String args[]) {
        System.out.println("This is your array :");

        char chosenboard[][] = createBoard(2);
        for (int row = 0; row < chosenboard.length; row++) {
            for (int column = 0; column < chosenboard[row].length; column++) {
                System.out.print(chosenboard[row][column]);
            }
        }
    }
}

Second attempt : doing "declare first, initialize later":

public class Apples {
    static char[][] createBoard(int boardType) {
        char[][] board;
        switch (boardType) {
            case 1:
                board = new char[][]{
                        {' ', '1', '2', '3', '4', '5', '6', '7', '8', '9'},
                        {'1', '#', '#', '#', '-', '@', '-', '#', '#', '#'},
                        {'2', '#', '#', '-', '@', '@', '@', '-', '#', '#'},
                        {'3', '#', '-', '@', '@', '-', '@', '@', '-', '#'},
                        {'4', '-', '@', '@', '@', '@', '@', '@', '-'}};
                break;
            case 2:
                board = new char[][]{
                        {' ', '1', '2', '3', '4', '5'},
                        {'1', '-', '-', '-', '-', '-'},
                        {'2', '-', '@', '@', '@', '-'},
                        {'3', '-', '-', '@', '-', '-'},
                        {'4', '-', '-', '@', '-', '-'},
                        {'5', '-', '-', '-', '-', '-'}};
                //here I am declaring and assigning again
                break;

        }
        return board;
    }

    public static void main(String args[]) {
        System.out.println("This is your array :");
        char chosenboard[][] = createBoard(2);
        for (int row = 0; row < chosenboard.length; row++) {
            for (int column = 0; column < chosenboard[row].length; column++) {
                System.out.print(chosenboard[row][column]);
            }
        }
    }
}

The previous error of "duplicate local variable" went away, but I got a new error on my return board line. It says the "the local variable board may not have been initialized"

1 Answers

Your code does not compile because the variable board is already defined before the switch statement and cannot be defined again (in that scope).

static char[][] createBoard(int boardType) {
  char[][] board = new char[8][10];          // <= declared and assigned
  switch (boardType) {
    case 1:
      char board[][] = {{' ', /* ... */ }};  // <= declared and assigned again
      break;
    // ...

Inside the case you can create a new 2d-array and then assign it to the already declared variable like this:

char[][] board; // declaration
board = new char[][]{{' ', '1', '2', '3', '4', '5', '6', '7', '8', '9'}, {'1', '#', '#', '#', '-', '@', '-', '#', '#', '#'}, {'2', '#', '#', '-', '@', '@', '@', '-', '#', '#'}, {'3', '#', '-', '@', '@', '-', '@', '@', '-', '#'}, {'4', '-', '@', '@', '@', '@', '@', '@', '-'}};

Looking a bit closer what your method createBoard does, you could simplify this further and directly construct and return the 2d-array:

static char[][] createBoard(int boardType) {
    switch (boardType) {
        case 1:
            return new char[][]{{' ', '1', '2', '3', '4', '5', '6', '7', '8', '9'}, {'1', '#', '#', '#', '-', '@', '-', '#', '#', '#'}, {'2', '#', '#', '-', '@', '@', '@', '-', '#', '#'}, {'3', '#', '-', '@', '@', '-', '@', '@', '-', '#'}, {'4', '-', '@', '@', '@', '@', '@', '@', '-'}};
        case 2:
            return new char[][]{{' ', '1', '2', '3', '4', '5'}, {'1', '-', '-', '-', '-', '-'}, {'2', '-', '@', '@', '@', '-'}, {'3', '-', '-', '@', '-', '-'}, {'4', '-', '-', '@', '-', '-'}, {'5', '-', '-', '-', '-', '-'}};
        default:
            return new char [8][10];
    }
}

Addressing your edit to the question which added a second attempt:
The error means that you are trying to return board but it might have not been initialised. In your case, if the input is not 1 or 2, there is no assignment to board. You can add a default case to the switch-statement to solve this. Or assign a default value when you declare the variable.

Related