Nikz11
Nikz11

Reputation: 13

Java: for loop ignoring conditions

My for loop is ignoring the !found field and carrying on even when found == true:

public int getDownYEntities() {
    int loc = 16;
    boolean found = false;
    EntityType eCheck;
    for (int col = locY; col < 17 || !found; col++) {
        eCheck = level[locX][col];
        System.out.println("Called");
        if ((eCheck == EntityType.ROCK) || (eCheck == EntityType.BOULDER) || (eCheck == EntityType.KEY) || (eCheck == EntityType.EXIT)) {
            switch (eCheck) {
                case ROCK:
                    loc = col - 2;
                    found = true;
                    break;
                case BOULDER:
                    loc = col - 2;
                    found = true;
                    System.out.println("Test1");
                    System.out.println(found);
                    break;
                case KEY:
                    loc = col - 2;
                    found = true;
                    break;
                case EXIT:
                    loc = col - 1;
                    found = true;
                    break;
            }
        }
        else
        {
            loc = col-1;
        }
    }
    return loc;
}

Log:

 I/System.out: Called
 I/System.out: Called
 I/System.out: Called
 I/System.out: Called
 I/System.out: Called
 I/System.out: Called
 I/System.out: Called
 I/System.out: Test1
 I/System.out: true
 I/System.out: Called
 I/System.out: Called
 I/System.out: Called
 I/System.out: Called
 I/System.out: Called
 I/System.out: Called
 I/System.out: Called
 I/System.out: Called
 I/System.out: Called
 I/System.out: Called

Am I being a complete idiot or something?

Upvotes: 0

Views: 278

Answers (1)

Scott Kennedy
Scott Kennedy

Reputation: 1346

col < 17 && !found

You want "and", not "or".

Upvotes: 6

Related Questions