-1

Possible Duplicate:
How do I compare strings in Java?

I am having a problem with the code below; the issue is that after you don't say map the first time, it keeps repeating No try again with the input box, no matter if you type "map" or not.

 System.out.println("My cousin Diego is in trouble in the Majestic palace. But how do we get there?");
 System.out.println("Who do we call when we don't know where to go?         Huh? I didn't get that...");
 imTheMap=robotMagic.next();
 if (imTheMap.equals("map"))
 {
    System.out.println("That's right!"); //the issue is here
 }
 else 
 {
    while(imTheMap!=("map"))
    {
        System.out.println("No! try again");
        imTheMap=robotMagic.next();
    }
 }
Community
  • 1
  • 1

1 Answers1

6

By changing

 while(imTheMap!=("map"))

to

 while(!imTheMap.equals("map"))

you always should use equals() method to check string equality. as in your if statement.

PermGenError
  • 45,977
  • 8
  • 87
  • 106