2

Possible Duplicate:
How do I compare strings in Java?

What am I doing wrong? After I compile and run the program, I type in my input and no matter what it is, the program always takes it as an incorrect input and says I'm wrong, here:

import java.util.Scanner;

public class mena3 {

    public static void main(String[] args) {
        Scanner user_input = new Scanner(System.in);

        String Capitol;
        System.out.print("Enter the capitol of Morocco: ");
        Capitol = user_input.next();

        if(Capitol == "Rabat") {
            System.out.println("Good Job!");
        }
        else {
            System.out.println("That is incorrect");
        }
    }
}

And after I put in Rabat, it says That is incorrect. If I put in l, it says That is incorrect. Why can't I win?

Community
  • 1
  • 1
William
  • 21
  • 3

3 Answers3

4

Don't compare Strings using ==. Use the equals(...) or the equalsIgnoreCase(...) method instead. Understand that == checks if the two objects are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. So instead of

if (fu == "bar") {
  // do something
}

do,

if ("bar".equals(fu)) {
  // do something
}

or,

if ("bar".equalsIgnoreCase(fu)) {
  // do something
}

Voting to close this question as it's only been asked and answered umpteen million times on this site.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Your typing speed is incredible. – nullpotent Sep 24 '12 at 00:40
  • 3
    He might already have this answered pre-written because of how often this question is asked >_ – nook Sep 24 '12 at 00:47
  • +1 speed. Yeah - the core of this question is probably be the most commonly asked question on this site (I answered one myself only yesterday). I'm gonna keep an answer handy so I can copy-paste it so I can "win" the race to answer :) – Bohemian Sep 24 '12 at 00:50
3

One of the most common mistakes in java. String require a .equals() rather than an ==.

Wrong:

if (str == "foo") {

}

Right:

if ("foo".equals(str)) { // done in this order to avoid NPE

}
Bohemian
  • 412,405
  • 93
  • 575
  • 722
nook
  • 2,378
  • 5
  • 34
  • 54
0

Your code is perfect, only your comparison method is wrong. All other languages treats == as comparison operator. But in case of Java it is little bit tricky. Here in Java == is taken as comparison operator for objects, not a string variable.

So, to compare two Strings you have a method called `.equals() which is from String class it self.

hence you need to change your code accordingly,

import java.util.Scanner;

public class mena3 
{
    public static void main(String[] args) 
    {
        Scanner user_input = new Scanner(System.in);

        String Capitol;
        System.out.print("Enter the capitol of Morocco: ");
        Capitol = user_input.next();

//        if(Capitol == "Rabat")  // your previous code
        if(Capitol .equals ( "Rabat") ) // new updated comparison code
        {
            System.out.println("Good Job!");
        }
        else 
        {
            System.out.println("That is incorrect");
        }
    }
}
Lucifer
  • 29,392
  • 25
  • 90
  • 143
  • You're a little slow... the other answers were there 7 minutes already before yours, and they say the same thing – Bohemian Sep 24 '12 at 00:53
  • Thanks guys, I'm sorry that this is another question asked way too frequently, thanks for putting up with it and answering. – William Sep 24 '12 at 01:07