0

The code below just keeps giving the else error message I don't know what's wrong..

Boolean loop2 = true;
userInput = scan.next();

while (loop2) {
    userInput = scan.next();
    if(userInput.toLowerCase() == "") {
        out.println(
            "Please pick a option! it's in the 'Commands' <= type this in!"
        );
        loop2 = true;
    } else if(userInput.toLowerCase() == "fight") { 
        Fights(); loop2 =   false;
    } else if(userInput.toLowerCase() == "train") { 
        Training(); loop2 = false; 
    } else if(userInput.toLowerCase() == "bosses") { 
        Bossing(); loop2 =  false; 
    } else if(userInput.toLowerCase() == "shops") { 
        ShopsAll(); loop2 = false; 
    } else if(userInput.toLowerCase() == "commands") { 
        Menu(); loop2 = false; 
    } else {
        out.println("ERROR MESSAGE!!.");
    }
}
Benny Hill
  • 6,191
  • 4
  • 39
  • 59
  • 1
    @R.J the problem is op didnt know that he/she having that problem – Baby Nov 29 '13 at 08:35
  • 1
    @RafaEl - But when the OP reads that a question named *How do I compare strings in Java?* is marked as a duplicate to this question, they'll figure out that it has to be something with string comparisons and looking into the solutions there and comparing with their own will solve it for them. – Rahul Nov 29 '13 at 08:37
  • @RafaEl I used to flag/vote-to-close with that mindset too, but the idea is that if some guy in the future stumbles on this question, there's nothing here that "How do I compare strings in Java?" doesn't already cover. This is a duplicate in the sense that it helps people learn about the exact same issue. – Dennis Meng Nov 30 '13 at 04:16

2 Answers2

0

use:

"fight".equals(userInput.toLowerCase());

== compares memory references

epoch
  • 16,396
  • 4
  • 43
  • 71
0

In java you must compare string with the .equals() or .equalsIgnoreCase() method. Never use == operator for comparing strings!

Lukas Warsitz
  • 1,231
  • 1
  • 11
  • 20