0

I have run the code multiple times in multiple scenarios and have gotten the same result. What it is supposed to do is move right, and when it hits the point it is supposed to turn, it turns. Instead, it gives me the error dialog box I instructed it to give when it hits something unidentified. It seems to be skipping the part of the code that tells it to execute a separate method. I have three other methods like this that do the same thing in different directions.

To detail how the situation works. I start the game and it loads the level with the player in it. I use some form of input that executes the code below. The character moves right, but then refuses to move even though the level says it should.

EDIT: I have amended the code to use .equals() instead of ==. I found that the problem actually arose with me forgetting how java does it's x and y axis. I thank those who fixed my flaw in coding though, it was really helpful.

public static void moveRight(){
    move = true;
    while(move == true){
        Point character = load.getCharacter();
        Point space = character;
        space.x += 20;
        String name = load.getSpace(space);
        System.out.println(name);
        if(name.equals("empty")){
            load.setCharacter(space);
        }else if(name.equals("fTurnUp")){
            load.setCharacter(space);
            move = false;
            moveUp();
        }else if(name.equals("fTurnDown")){
            load.setCharacter(space);
            move = false;
            moveDown();
        }else if(name.equals("fTurnLeft")){
            load.setCharacter(space);
            move = false;
            moveLeft();
        }else if(name.equals("fTurnRight")){
            load.setCharacter(space);
            move = false;
            moveRight();
        }else if(name.equals("turn")){
            load.setCharacter(space);
            move = false;
        }else if(name.equals("finish")){
            load.setCharacter(space);
            move = false;
            Component frame = null;
            JOptionPane.showMessageDialog(frame,
                    "Congratulations! You have completed the level.",
                    "Level Completed",
                    JOptionPane.PLAIN_MESSAGE);
        }else if(name.equals("wall")){
            move = false;
            Component frame = null;
            JOptionPane.showMessageDialog(frame,
                    "The level was not designed correctly. Click 'Ok' to return to main menu",
                    "Invalid Level",
                    JOptionPane.ERROR_MESSAGE);
        }
    }
}
  • At least you should use `name.equals("empty")` instead of `name == "empty"` in all situations where string comparison appears – Dmitry P. Jan 28 '16 at 21:26

3 Answers3

1

The difference in that for string comparison, you have to use the following:

if(name.equals("empty")) {
   load.setCharacter(space);
}

When you use ==, you are comparing the reference which the values are pointing to whereas equals() is comparing the actual value.

catch23
  • 17,519
  • 42
  • 144
  • 217
1

Try changing your comparator from == to .equals(). The former is pointing to the references of the string whereas the latter actually compares the value.

So given that, it should be:

if(name.equals("empty")){
        load.setCharacter(space);
    }else if(name.equals("fTurnUp")){
        load.setCharacter(space);
        move = false;
        moveUp();
    }else if(name.equals("fTurnDown")){
        load.setCharacter(space);
        move = false;
        moveDown();
    }else if(name.equals("fTurnLeft")){
        load.setCharacter(space);
        move = false;
        moveLeft();
    }else if(name.equals("fTurnRight")){
        load.setCharacter(space);
        move = false;
        moveRight();
    }else if(name.equals("turn")){
        load.setCharacter(space);
        move = false;
    }else if(name.equals("finish")){
        load.setCharacter(space);
        move = false;
        Component frame = null;
        JOptionPane.showMessageDialog(frame,
                "Congratulations! You have completed the level.",
                "Level Completed",
                JOptionPane.PLAIN_MESSAGE);
    }else if(name.equals("wall")){
        move = false;
        Component frame = null;
        JOptionPane.showMessageDialog(frame,
                "The level was not designed correctly. Click 'Ok' to return to main menu",
                "Invalid Level",
                JOptionPane.ERROR_MESSAGE);
    }
sparkhee93
  • 1,381
  • 3
  • 21
  • 30
0

Try to use

     MyString.equals("otherstring")

And not == to compare strings in your if statement.

Also, for boolean, no need to write ==, just use while(move)

Derlin
  • 9,572
  • 2
  • 32
  • 53