0

So, I was programming a very simple text based game, when it came to my attention that my while statement was exempting a crucial part of the game. The brackets were all correct. Maybe one of you can help me?

Here is the code:

if (isActionPile .equals("Attack Over")){
        System.out.println("\n\nWelcome to the Travel Menu. Here you can decide where to travel, what items\n" +
                "and upgrades you wish to buy, and of course, see you inventory!");
        while (isWhileStatementAssist != "Done") {
            System.out.println(isTravelMenu);
            isActionPile = Scan.nextLine();
            if (isActionPile.equals("Item Shop")) {
            //This part doesn't matter
                }
//This is the trouble spot!
            if ((isActionPile.equals("Character Menu"))) {
                System.out.println("Your attack is " + isArthurAttack + ", your Health Points are at " +
                        isArthurHealth + " and you have $" + isArthurMoney + "\nTo leave this menu press enter.");
                isActionPile = Scan.nextLine();
                isActionPile = null;
                isWhileStatementAssist = null;
            }
            if (isActionPile.equals("Movement Menu")) {
            //I haven't started this part yet!
            }
            }
        }

So, I even went the extra mile to add the part that says "isWhileStatementAssist = null;". Can any of you see the problem?

Thanks, Dave

DA_
  • 3
  • 3
  • 3
    The offending code: `while (isWhileStatementAssist != "Done") {` Never use `==` or `!=` to compare Strings. These check for *reference* equality, that two references refer to the same object. Instead what you really want to test is *functional* equality, that two Strings have the same chars in the same order, and for this use `.equals(...)` or `.equalsIgnoreCase(...)`. – Hovercraft Full Of Eels Jul 12 '17 at 19:58
  • You also have a problem with isActionPileEquals("") since it is null and not a string. I don't have the link to Java conventions, but it should be "".equals(isActionPile) – Berkley Lamb Jul 12 '17 at 20:05

0 Answers0