0

I have a method that should return a enum, however I am not to sure how to code this (it is a activity from my paper). Below is the method (in the class called Maze):

private MazeStatus[][] stringToMaze(String sMaze) {
    String[] splitString = sMaze.split("\n");
    char[][] array = new char[splitString.length][];
    MazeStatus[][] mazeStat;

    for (int x = 0; x < splitString.length; x++) {
        array[x] = new char[splitString[x].length()];
        array[x] = splitString[x].toCharArray();
        for (int y = 0; y < splitString[x].length(); y++) {
            switch (array[x][y]) {
                case '.':
                    mazeStat[x][y] = MazeStatus.VISITED;
                    break;
            }
        }
    }
    return null; // TO DO (Part 1): change appropriately
}

Here is the MazeStatus class:

public enum MazeStatus {
    OPEN(' '), OBSTACLE('#'), GOAL('x'), VISITED('.');

    private char text;

    MazeStatus(char s) {
        this.text = s;
    }

    public char text() {
        return this.text;
    }
}

In the method I have tried creating a enum

MazeStatus[][] mazeStat;

and adding values to it:

mazeStat[x][y] = MazeStatus.VISITED;

But of course it complains about it not being initialized. I do not know how to initialize a enum, especially a bidimensional array enum, without creating the actual Maze object.

Community
  • 1
  • 1
DaleJV
  • 370
  • 1
  • 3
  • 15
  • `mazeStat` is not an enum, it is a 2D object array, and needs to be initialized the same way all other 2D arrays are initialized, i.e. using `new MazeStatus[...][...]` – Andreas Apr 06 '20 at 07:59

2 Answers2

4

You need to define the size of your 2D array, like this:

MazeStatus[][] mazeStat = new MazeStatus[row.length][column.length];

I don't know exactly what the size will be in your case, that's why I've used row.length and column.length. But please replace these with values that make sense in your code.

Alexandru Somai
  • 1,395
  • 1
  • 7
  • 16
  • Thank you, this does answer my question! Just so used to ussing ArrayList now a days so forgot about having to initialize all arrays. However I do now run into a issue. The user can input a String that represents a maze and what I am doing is converting that String into a MazeStatus[][] array. So I can set the first part by doing MazeStatus[splitString[x].length()] but I am not sure what to put in the second [] as each line can be a different size. Could have 5 columns but each row is a different size so not sure how to initialize it in that case. Sorry still new to 2d arrays! – DaleJV Apr 06 '20 at 07:57
  • 1
    Well, you could ask the user to input the maze size at first? And then use these parameters to initialize your 2D array. Otherwise, you could replace the 2D array with a `List mazeStat = new ArrayList<>()`, and use the `List` API to build your maze. Then, when returning, just convert from 2D List to 2D Array. – Alexandru Somai Apr 06 '20 at 08:35
  • Just checked and turns out all inputted mazes will always have the same size, as in all rows will be the same size! So I will just get the first rows length and use it to initialize the second part of the array! I am having a issue doing so though. When I do MazeStatus[][] mazeStat = new MazeStatus[splitString.length][array[0].length()]; the compiler says "cannot find symbol" and points at the dot after array[0]? – DaleJV Apr 06 '20 at 08:48
  • 1
    `length` is a property on the array, not a function. I think it should be `array[0].length`. – Alexandru Somai Apr 06 '20 at 08:50
  • That has worked, thank you once again. Could you explain why for the char 2d array I have to do .length without () yet with String 2d array I have to do .length() please? – DaleJV Apr 06 '20 at 08:52
  • 1
    This should answer your question https://stackoverflow.com/questions/23396421/when-to-use-length-vs-length. Or even more complex answer, this one: https://stackoverflow.com/questions/23730092/how-can-i-get-the-size-of-an-array-a-collection-or-a-string-in-java – Alexandru Somai Apr 06 '20 at 08:58
1
private MazeStatus[][] stringToMaze(String sMaze) {
    String[] splitString = sMaze.split("\n");
    char[][] array = new char[splitString.length][];

    //this is missing in your code ........
    MazeStatus[][] mazeStat = new MazeStatus[splitString.length][column.length];
    for (int x = 0; x < splitString.length; x++) {
        array[x] = new char[splitString[x].length()];
        array[x] = splitString[x].toCharArray();
        for (int y = 0; y < splitString[x].length(); y++) {
            switch (array[x][y]) {
                case '.':
                    System.out.println("x" + x + " y" + y);
                    mazeStat[x][y] = MazeStatus.VISITED;
                    break;
                default:
                    mazeStat[x][y] = MazeStatus.OPEN;
                    /*
                     *this you have to put accordingly because if default 
                     *condition won't be there then it will take null
                     *values and it will throw NullPointerException
                     */
            }
        }
    }
    return mazeStat; // TO DO (Part 1): change appropriately
}
Community
  • 1
  • 1
Bablu Singh
  • 104
  • 8