0

I am making a maze solver. For some reason Everytime the line marked with '-->' is reached, "Enter height: " is outputted. It is like that line (which is not run when it is reached) somehow makes the method loop.

private void makeMap() {
    Map map; //used to convert the char array into a graph
    int height; //the height of the map by user input
    char[][] array; //used to store the char map

    System.err.println("Enter height: ");
    height = scanner.nextInt(); //gets and stores the height from the user

    array = new char[height][]; //initializes the map with input height
    for(int i=0; i<height; i++) { //adds row by row to the map array
        System.err.print("Enter next line of map: ");
        array[i] = scanner.next().toCharArray();
    }

    --> map = new Map(array); //initializes the map by passing the char array

    graph = map.makeGraph(); //creates a graph from the char array
  }

I labelled with '-->' where I believe my problem lays. Any code i put before the marked line will execute, but as soon as that line is reached it loops back to the top of this method. Below is the Map constructor:

public Map(char[][] passMap) {
    adjList = new Vertex[map.length*map[0].length];
    map = passMap; //stores the passed map
}

ANY HELP is better than no help. I've been at this for hours. Thanks.

5
  • There's a "bug" with the Scanner class. You can try doing a next after a nextInt and nextFloat. See this topic and RD1's comment for more information: stackoverflow.com/questions/4708219/… Commented Jul 1, 2011 at 16:59
  • Inside the constructor you have a variable map. Is this a private field of the class? Commented Jul 1, 2011 at 17:00
  • What do you mean by "loop back to the top of the method?" Commented Jul 1, 2011 at 17:00
  • Is there a particular reason for using System.err.println for your output? Commented Jul 1, 2011 at 17:01
  • From what you have posted it is impossible for the code to loop back to "Enter Height" line unless or until makeMap is being called from a loop.So it will be better if you look into part from where makeMap is being called or provide us with more comprehensive snippet for us to look into it. Commented Jul 1, 2011 at 17:16

2 Answers 2

1

Your map variable is probably uninitialized. Change:

adjList = new Vertex[map.length*map[0].length];

To:

adjList = new Vertex[passMap.length*passMap[0].length];

I would also change your System.err.println() calls to System.out.println(). The first one is for error output, and the second one for normal console output.

Sign up to request clarification or add additional context in comments.

1 Comment

WOW thanks a ton! all i had to do is switch my two instance variables! I've literally been at this for several hours. You're da bomb!! (and thanks for the .err tip)!
0

Why not learn to use a debugger (it's pretty easy) and just set a breakpoint on that line. That would give you your answer real quick.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.