public class MazeArray {
    public static void main(String[] args) throws FileNotFoundException{
        File inputFile = new File("maze1.txt");
        Scanner sc = new Scanner(inputFile);
        int maze[][] = null;

        maze = createMaze(sc, maze);

        mazePathFinder(sc, maze);
        printMaze(maze);

    }

    public static int[][] createMaze(Scanner sc, int[][] maze){
        int arrayRowSize = sc.nextInt();
        int arrayColumnSize = sc.nextInt();
        maze = new int[arrayRowSize][arrayColumnSize];
        for(int i = 0; i < arrayRowSize; i++){
            for(int j = 0; j < arrayColumnSize; j++){
                maze[i][j] = sc.nextInt();
                }
        }
        return maze;
    }


    public static void printMaze(int[][] maze){
        for(int i = 0; i < maze.length; i++){
            for(int j = 0; j < maze[i].length; j++){
                System.out.print(maze[i][j]);
            }
            System.out.println();
        }
    }

    public static void mazePathFinder(Scanner sc, int[][] maze){
        Stack mazeStack = new Stack();
        int count = 0;
        boolean done = false;
        String rowCoordinates = sc.next();
        String columnCoordinates = sc.next();
        int row = Integer.parseInt(rowCoordinates);
        int column = Integer.parseInt(columnCoordinates);
        Coordinate startingCoord = new Coordinate(row, column);
        mazeStack.push(startingCoord);
        count++;
        maze[row][column] = 1;

        //Looks North, if the value is a 0, converts it to 1 and pushes coordinates onto the stack
        try{
            if(maze[row - 1][column] == 0){
                row -= 1;
                maze[row][column] = 1;
                Coordinate secondCoord = new Coordinate(row, column);
                mazeStack.push(secondCoord);
                count++;
            }

        }
        catch(ArrayIndexOutOfBoundsException aiooe){
            }
        //Looks East
        try{
            if(maze[row][column + 1] == 0){
                column += 1;
                maze[row][column] = 1;
                Coordinate secondCoord = new Coordinate(row, column);
                mazeStack.push(secondCoord);
                count++;

            }
        }
        catch(ArrayIndexOutOfBoundsException aiooe){
        }
        //Looks South
        try{
            if(maze[row + 1][column] == 0){
                row += 1;
                maze[row + 1][column] = 1;
                Coordinate secondCoord = new Coordinate(row, column);
                mazeStack.push(secondCoord);
                count++;
            }
        }
        catch(ArrayIndexOutOfBoundsException aiooe){
        }
        //Looks West
        try{
            if(maze[row][column - 1] == 0){
                column -= 1;
                maze[row][column] = 1;
                Coordinate secondCoord = new Coordinate(row, column);
                mazeStack.push(secondCoord);
                count++;
            }
        }
        catch(ArrayIndexOutOfBoundsException aiooe){
        }

        try{
            while(!done){
                Coordinate nextCoord = new Coordinate(row, column);
                //Pushes nextCoord to stack if == 0
                if (maze[row - 1][column] == 0){
                    maze[row - 1][column] = 1;
                    row -= 1;
                    mazeStack.push(nextCoord);
                    count++;
                    continue;

                }
                //Pushes nextCoord to stack if == 0
                else if(maze[row][column + 1] == 0){
                    maze[row][column + 1] = 1;
                    column += 1;
                    mazeStack.push(nextCoord);
                    count++;
                    continue;
                }

                //Pushes nextCoord to stack if == 0
                else if(maze[row + 1][column] == 0){
                    maze[row + 1][column] = 1;
                    row += 1;
                    mazeStack.push(nextCoord);
                    count++;
                    continue;
                }
                 //Pushes nextCoord to stack if == 0
                else if(maze[row][column - 1] == 0){
                    maze[row][column - 1] = 1;
                    column -= 1;
                    mazeStack.push(nextCoord);
                    count++;
                    continue;
                }
                mazeStack.pop();
                row = nextCoord.getRow();
                column = nextCoord.getColumn();
                break;
            }
        }
        catch(ArrayIndexOutOfBoundsException aiooe){
            done = true;
        }

        Stack reorderCoords = new Stack();
        for(int i = 0; i < count - 1; i++){
            reorderCoords.push(mazeStack.pop());
        }

        for(int i = 0; i < count - 1; i++){
            System.out.println(reorderCoords.pop());
        }
    }

}
Comments
can you show the output?

Hi all - so the end goal is to have a program that reads in the size of a 2d array, the contents of the 2d array, and the starting coordinates and then navigates through the maze using stacks. The output right now is as follows:

The program prints out the proper path that it takes through the maze, while popping out coordiantes that do not lead to the correct path. It changes the 0s to 1s after it has pushed that coordiante onto the stack. I can see where the program gets stuck, at < 2 , 4>..but I can also see that the output of the coordinates from the stack is off..any advice would be great.

<1,0>
<1,1>
<1,1>
<1,2>
<1,3>

555555
111115
550515
550555
500000
555555

Add lots of println statements to print out the values of variables used to contol the program's execution.

You need to explain what is wrong with the current output and post an exampleof what the output should look like.

The posted code does not compile without errors. One problem is missing import statements.

Edited 4 Years Ago by NormR1

I changed the maze too:

555555
000555
550555
550555
550000
555555

Just to see if it gets stuck when it doesn't have to back up, and it doesn't..so I am suspecting it has something to do with the while(!done) loop...

Continue debugging by printing out the variables used in the loop

You should NOT ignore array out of bounds exceptions (line 143). They should NEVER happen in a debugged program. You need to remove the try/catch that ignores them and fix the code!!!

Edited 4 Years Ago by NormR1

I must have missed the import statements when I uploaded the code, there are import statements in my code and it does still compile without errors. The output of the maze should be:

555555
111115
551515
551555
501111
555555

<1,0>
<1,1>
<1,2>
<2,2>
<3,2>
<4,2>
<4,3>
<4,4>
<4,5>

I have already posted the output I have been getting.

You should NOT ignore array out of bounds exceptions (line 143). They should NEVER happen in a debugged program. You need to remove the try/catch that ignores them and fix the code!!!

For some reason there is a delay between your post showing up on here, not sure what is going on so I apologize. I added println(row) and println(column) statements to each of the Try fields. The output is:

1 1

1 2

1 3

1 4

2 4

And I don't know if it makes a big difference to you, but the ArrayOutOfBoundsException is how we are supposed to know that we have found the exit to the maze.

Edited 4 Years Ago by ThisIsMeOrIsIt: Changed output display slightly.

A list of numbers isn't very useful. The printlns should include the variable name:
System.out.println("varName"+varName);

ArrayOutOfBoundsException is how we are supposed to know that we have found the exit to the maze.

That sounds dumb. The code should detect when the index goes past the end of the array.

Add a call to printStackTrace() while debugging the code to be sure that you know what caused the exception.

Edited 4 Years Ago by NormR1

From the output, I can see where the program is getting stuck..which is a the coordinate <2,4>..but I don't know how to pop the previous coordinates off the stack and than find the next open path. (the 0)

I can't compile and test the code as is. Fix the print outs so they show the varialbe names
and fix the try/catch block so you see where the error happens.

package programfour;


import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import stackpackage.*;

public class MazeArray {
    public static void main(String[] args) throws FileNotFoundException{
        File inputFile = new File("maze1.txt");
        Scanner sc = new Scanner(inputFile);
        int maze[][] = null;

        maze = createMaze(sc, maze);

        mazePathFinder(sc, maze);
        printMaze(maze);

    }

    public static int[][] createMaze(Scanner sc, int[][] maze){
        int arrayRowSize = sc.nextInt();
        int arrayColumnSize = sc.nextInt();
        maze = new int[arrayRowSize][arrayColumnSize];
        for(int i = 0; i < arrayRowSize; i++){
            for(int j = 0; j < arrayColumnSize; j++){
                maze[i][j] = sc.nextInt();
                }
        }
        return maze;
    }


    public static void printMaze(int[][] maze){
        for(int i = 0; i < maze.length; i++){
            for(int j = 0; j < maze[i].length; j++){
                System.out.print(maze[i][j]);
            }
            System.out.println();
        }
    }

    public static void mazePathFinder(Scanner sc, int[][] maze){
        Stack mazeStack = new Stack();
        int count = 0;
        boolean done = false;
        int row = sc.nextInt();
        int column = sc.nextInt();
        Coordinate startingCoord = new Coordinate(row, column);
        mazeStack.push(startingCoord);
        count++;
        maze[row][column] = 1;

        //Looks North, if the value is a 0, converts it to 1 and pushes coordinates onto the stack
        try{
            if(maze[row - 1][column] == 0){
                row -= 1;
                maze[row][column] = 1;
                Coordinate nextCoord = new Coordinate(row, column);
                mazeStack.push(nextCoord);
                count++;
                System.out.println("row:" + row + " " + "column:" + column);
            }

        }
        catch(ArrayIndexOutOfBoundsException aiooe){
            aiooe.printStackTrace();
            }
        //Looks East
        try{
            if(maze[row][column + 1] == 0){
                column += 1;
                maze[row][column] = 1;
                Coordinate nextCoord = new Coordinate(row, column);
                mazeStack.push(nextCoord);
                count++;
                System.out.println("row:" + row + " " + "column:" + column);
            }
        }
        catch(ArrayIndexOutOfBoundsException aiooe){
            aiooe.printStackTrace();
        }
        //Looks South
        try{
            if(maze[row + 1][column] == 0){
                row += 1;
                maze[row + 1][column] = 1;
                Coordinate nextCoord = new Coordinate(row, column);
                mazeStack.push(nextCoord);
                count++;
                System.out.println("row:" + row + " " + "column:" + column);
            }
        }
        catch(ArrayIndexOutOfBoundsException aiooe){
            aiooe.printStackTrace();
        }
        //Looks West
        try{
            if(maze[row][column - 1] == 0){
                column -= 1;
                maze[row][column] = 1;
                Coordinate nextCoord = new Coordinate(row, column);
                mazeStack.push(nextCoord);
                count++;
                System.out.println("row:" + row + " " + "column:" + column);
            }
        }
        catch(ArrayIndexOutOfBoundsException aiooe){
            aiooe.printStackTrace();
        }

        try{
            while(!done){
                Coordinate nextCoord = new Coordinate(row, column);
                //Pushes nextCoord to stack if == 0
                if (maze[row - 1][column] == 0){
                    maze[row - 1][column] = 1;
                    row -= 1;
                    mazeStack.push(nextCoord);
                    count++;
                    System.out.println("row:" + row + " " + "column:" + column);
                    continue;

                }
                //Pushes nextCoord to stack if == 0
                else if(maze[row][column + 1] == 0){
                    maze[row][column + 1] = 1;
                    column += 1;
                    mazeStack.push(nextCoord);
                    count++;
                    System.out.println("row:" + row + " " + "column:" + column);
                    continue;
                }

                //Pushes nextCoord to stack if == 0
                else if(maze[row + 1][column] == 0){
                    maze[row + 1][column] = 1;
                    row += 1;
                    mazeStack.push(nextCoord);
                    count++;
                    System.out.println("row:" + row + " " + "column:" + column);
                    continue;
                }
                 //Pushes nextCoord to stack if == 0
                else if(maze[row][column - 1] == 0){
                    maze[row][column - 1] = 1;
                    column -= 1;
                    mazeStack.push(nextCoord);
                    count++;
                    System.out.println("row:" + row + " " + "column:" + column);
                    continue;
                }
                mazeStack.pop();
                row = nextCoord.getRow();
                column = nextCoord.getColumn();
                break;
            }
        }
        catch(ArrayIndexOutOfBoundsException aiooe){
            aiooe.printStackTrace();
            done = true;
        }

        Stack reorderCoords = new Stack();
        for(int i = 0; i < count - 1; i++){
            reorderCoords.push(mazeStack.pop());
        }

        for(int i = 0; i < count - 1; i++){
            System.out.println(reorderCoords.pop());
        }
    }

}

Above is my entire code as is...the output is:

row:1 column:1
row:1 column:2
row:1 column:3
row:1 column:4
row:2 column:4
<1,0>
<1,1>
<1,1>
<1,2>
<1,3>
555555
111115
550515
550555
500000
555555

This is the stack class

    package stackpackage;

    import java.util.LinkedList;

    public class Stack {
        private LinkedList stack;

        public Stack() {
            stack = new LinkedList();
        }

        public boolean isEmpty() {
            return stack.size() == 0;
        }

        public Object pop() {
            if (isEmpty()) {
                throw new EmptyStackException();
            }
            return stack.removeFirst();
        }

        public void push(Object obj) {
            stack.addFirst(obj);
        }

        public Object peek() {
            if (isEmpty()) {
                throw new EmptyStackException();
            }
            return stack.peek();
        }
    }
Comments
can you show the output?
    package programfour;

    public class Coordinate {
        private Integer row;
        private Integer column;

        public Coordinate(Integer row, Integer column){
            this.row = row;
            this.column= column;
        }

        public void setRow(Integer row){
            this.row = row;
        }

        public void setColumn(Integer column){
            this.column = column;
        }

        public Integer getRow(){
            return row;
        }

        public Integer getColumn(){
            return column;
        }

        public String toString(){
            return "<" + row.toString() + "," + column.toString() + ">";
        }

    }

Above is the main class, the stack class, and the coordinate class...I am not sure how to edit the try and catch blocks to find the error..

Add a call to the printStackTrace() method to the catch block. Its print out will show you where the exception happened and the value of the index.

That's it for tonight. Back tomorrow.

ChaiNeeys: this is a three year old (Dead) Thread. let it rest. If you have questions of your own, post them in a new thread.
If you want to see the output of the above code, run it.

This article has been dead for over six months. Start a new discussion instead.