Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why don't strings compare as equal? [duplicate]

Tags:

java

Possible Duplicate:
String is not equal to string?

I'm new to java and I can't figure out what's wrong with this code block. I know the array isn't null I'm testing it elsewhere. Maybe there is a syntax problem I'm used to program in c#.

     Scanner input = new Scanner(System.in);
     System.out.println("Enter ID :");
     String employeeId = input.nextLine();
     int index =  -1;
     for(int i = 0 ; i < employeeCounter ; i++)
     {
         if(employeeId == employeeNumber[i])
         {
           index = i;
         }
     }

     if(index == -1)
     {
         System.out.println("Invalid");
         return;
     }

I always get to the 'Invalid' part. Any idea why ? Thanks in advance


employeeNumber[0] is "12345" employeeId is "12345" but I can,t get into the first if statement although employeeId IS equal to employeeNumber[0].

like image 261
phadaphunk Avatar asked Apr 25 '12 05:04

phadaphunk


People also ask

Why can't we use == to compare string objects?

You should not use == (equality operator) to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not. On the other hand, equals() method compares whether the value of the strings is equal, and not the object itself.

Can two strings be compared to be equal?

The equals() method compares two strings, and returns true if the strings are equal, and false if not. Tip: Use the compareTo() method to compare two strings lexicographically.

Can you compare two strings using the == operator?

The Java equals() method compares two string objects, the equality operator == compares two strings, and the compareTo() method returns the number difference between two strings. String comparison is a crucial part of working with strings in Java.

Why use .equals instead of == Java?

The == operator can't compare conflicting objects, so at that time the compiler surrenders the compile-time error. The equals() method can compare conflicting objects utilizing the equals() method and returns “false”.


3 Answers

Don't compare strings with ==.

Use

if (string1.equals("other")) {
    // they match
}
like image 128
John3136 Avatar answered Oct 30 '22 10:10

John3136


Compare strings like that

if(employeeId.equals(employeeNumber[i]) {

}
like image 34
juergen d Avatar answered Oct 30 '22 09:10

juergen d


As others have pointed - full code will be helpful, but my guess would be this line of the code:

if(employeeId == employeeNumber[i])

You don't compare 2 strings by using ==. Use equals() or equalsIgnoreCase() instead. == only checks for object equality i.e. are employeeId and employeeNumber referencing to the same object in memory. So, for objects always use the equals() method..for Strings you can also use equalsIgnoreCase() for a case insensitive match. == should be used on primitive types like int, long etc.

like image 42
legendofawesomeness Avatar answered Oct 30 '22 11:10

legendofawesomeness