Search code examples
javaeclipseconsoleeclipse-mars

Nothing is being output to the console! (Java, Eclipse Mars)


So basically I am experimenting with writing a path finding program that find a path from some point in a 10*10 grid to another, that is fine.

I have a class Path that is an ArrayList of GridSquares (which are just glorified co-ordinates).

I have written a small method within the Path class to display the path, and this is where the problem, which is so minor but so very infuriating, arises.

When I try to run the code, and call displayPath, nothing is output to the console and the program terminates with no errors.

Here is the code for displayPath:

public void displayPath(){
    System.out.println("This is displayPrint"); //This line is included to make sure the program calls the method correctly.
    for(int i=1; i==10; i++){
        for(int j=1; j==10; j++){
            if(this.includesSquare(i, j)){
                System.out.print("[x]");
            } else {
                System.out.print("[ ]");
            }
        }
        System.out.print("\n");
    }
}

I included the first line to ensure that the console/System.out.print() was working correctly and this gets displayed every time the method is called.

Here is the code for includesSquare:

public boolean includesSquare(int x, int y){
    for(GridSquare square : this.path){
        if(square.getX()==x && square.getY()==y){
            return true;
        }
    }
    return false;
}

I have uninstalled and re-installed Eclipse, copied the java files into a new project ect and nothing seems to make any difference. I know the console is working fine as it displays the first line of displayPath correctly.

Any help is greatly appreciated!


Solution

  • for(int i=1; i==10; i++) and for(int j=1; j==10; j++) will not work.

    The middle condition (i==10) is supposed to say when the loop is supposed to be executed. As it is, you are saying you only want the loop to execute when i is equal to 10. Since i is initially equal to 1, it will skip right over the loop.

    What you will likely want is

    for(int i=1; i<10; i++)
    

    This way, when i is equal to 1, it satisfies the condition that it is less than 10, so the loop will get executed and i will increment. This will keep happening until i is equal to 10, at which point the condition i<10 fails, so the loop will exit.

    In less words, you want your condition to say "loop while this is true" as opposed to "loop until this is true".