0

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!

greg-449
  • 109,219
  • 232
  • 102
  • 145
  • Is this: `\\This line is included [...]` part of your code? Because you comment with forward slashes, not backslashes. – CubeJockey Oct 01 '15 at 19:54
  • no added that in as part of the question! my mistake! –  Oct 01 '15 at 19:55
  • What is your intent with `for(int i=1; i==10; i++)` ? Specifically the `i == 10` condition – CubeJockey Oct 01 '15 at 19:55
  • basically I want the loop to finish when i or j==10, is this not possible with a for loop, I was not aware! –  Oct 01 '15 at 19:58
  • change the condition to `j<=10`.With your code you are saying that execute the loop only if `j=10` and nothing else – singhakash Oct 01 '15 at 19:59
  • You can, but instead that condition is moreso used to express when you want the loop to keep running. When you initialize `i` to 1, it won't be `==` to 10, and so the loop doesn't run even once :). Instead you want `i < 10` so that your loop runs as long as `i` is less than 10. And as you say, it will terminate when i == 10. – CubeJockey Oct 01 '15 at 19:59
  • Wow you uninstalled and re-installed? Come on the site and let us look it over before going as extreme as uninstalling/installing. :) We are here to help. – gonzo Oct 01 '15 at 20:00
  • it is an easy fix, can be replaces with i<11 but i didn't see the issue with using == as they both resolve to a Boolean anyway. Ahhh @Trobbins that makes sense! –  Oct 01 '15 at 20:00
  • yeah! @gonzo i read elsewhere that people with a similar problem had managed to solve by reinstalling! :P –  Oct 01 '15 at 20:03

2 Answers2

3

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".

gla3dr
  • 2,179
  • 16
  • 29
  • Was gonna edit that in after. Patience @MarounMaroun! :P – gla3dr Oct 01 '15 at 19:55
  • 3
    @gla3dr you're allowed to wait until your answer is complete before posting your answer. – CubeJockey Oct 01 '15 at 19:56
  • They won't work because if `i=1`, then `i==10` is false, and the loop will terminate. Please add an explanation to your answer. – Maroun Oct 01 '15 at 19:59
  • @gla3dr Thanks! for some reason i was thinking about the middle statement as a sort of 'go until this' statement which doesn't actually make a whole lot of sense, but id just never thought about it before! –  Oct 01 '15 at 20:05
  • @Trobbins When you say I am allowed to, do you mean I should? – gla3dr Oct 01 '15 at 21:53
1

for(int i=1; i==10; i++){ is where your problem lies.

The syntax for the for loop is as follows:

for(<initial condition>; <checking condition>; <incrementing>)

So what you have is

Staring from i = 1, increment by 1 while i == 10. Well since i starts at 1, you've already failed at the first step!

Turn your for loop into while loop to understand this better:

int i = 1;
while(i == 10) {
    doSomething();
    i++;
}

So of course that won't work.

Alex K
  • 8,269
  • 9
  • 39
  • 57