Qing Li
Qing Li

Reputation: 21

how to have one return statement in one method in java?

I know how to have one return in a method in some easy situation, but when a method return an object from another class, I don't know how to give a variable to store the return value. The specific code showed below:

public Path findPath(Maze maze) {
    Path path = new Path();
    MazePosition initialPos = new MazePosition(0, 0, null);
    Stack<MazePosition> posForExplore = new Stack<MazePosition>();
    MazePosition pos = initialPos;
    MazePosition next;

    posForExplore.push(pos);
    while (!posForExplore.empty()) {
        pos = posForExplore.pop();
        switch (maze.getPosStatus(pos)) {
        case GOAL:
            MazePosition p = pos;
            while (p!=null) {
                path.insertFirst(p.getCoords()[0], p.getCoords()[1]);
                p =p.getFrom();
            }
            return path;
        case VISITED:
            break;
        case OBSTACLE:
            break;
        case OPEN:
            maze.setPosStatus(pos, MazeStatus.VISITED);
            for (Movement mov: DIRS_TO_EXPLORE) {
                next = maze.getNeighbour(pos, mov);
                if (next!=null&&(maze.getPosStatus(next)==MazeStatus.OPEN||maze.getPosStatus(next)==MazeStatus.GOAL)) {
                    posForExplore.push(next);
                }
            }
            break;
        }
    }
    return null;
}

I have a return path and return null, so how can I only have one return statement. Thank you so much!

Upvotes: 0

Views: 95

Answers (3)

Andreas
Andreas

Reputation: 159096

To eliminate return path; from the middle of the method, and only have a single return statement at the end, you need to be able to exit the loop from where the return path; is, and you need to specify that return value should not be null.

Path returnPath = null;
LOOP: while (!posForExplore.empty()) {
    ...
    switch (maze.getPosStatus(pos)) {
        case GOAL:
            ...
            returnPath = path;
            break LOOP;
        ...
    }
}
return returnPath;

Upvotes: 0

Pankaj Gadge
Pankaj Gadge

Reputation: 2814

Here are the steps you must take

  • Try to initialize new Path instance to null at the beginning of your method e.g. pathReturn
  • In each switch statement, before doing break, assign pathReturn to your result and then break
  • At the end of your method, just return pathReturn instead of null

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201437

Declare and initialize path to null. Check for null before you insert anything and initialize it then (breaking the loop to trigger the return). At the end return path. Like,

public Path findPath(Maze maze) {
    Path path = null;
    MazePosition initialPos = new MazePosition(0, 0, null);
    Stack<MazePosition> posForExplore = new Stack<MazePosition>();
    MazePosition pos = initialPos;
    MazePosition next;

    posForExplore.push(pos);
    loop: while (!posForExplore.empty()) {
        pos = posForExplore.pop();
        switch (maze.getPosStatus(pos)) {
        case GOAL:
            MazePosition p = pos;
            while (p != null) {
                if (path == null) {
                    path = new Path();
                }
                path.insertFirst(p.getCoords()[0], p.getCoords()[1]);
                p = p.getFrom();
            }
            break loop;
        case VISITED:
            break;
        case OBSTACLE:
            break;
        case OPEN:
            maze.setPosStatus(pos, MazeStatus.VISITED);
            for (Movement mov : DIRS_TO_EXPLORE) {
                next = maze.getNeighbour(pos, mov);
                if (next != null && (maze.getPosStatus(next) == MazeStatus.OPEN
                        || maze.getPosStatus(next) == MazeStatus.GOAL)) {
                    posForExplore.push(next);
                }
            }
            break;
        }
    }
    return path;
}

Upvotes: 2

Related Questions