Why don't strings compare as equal?

Possible duplicate:
Is the string not equal to the string?

I am new to java and I can't figure out what happened to this block of code. I know the array is not null. I am testing it elsewhere. There may be a syntax issue that I use for programming 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] "12345" employeeIdthere is "12345" but I cannot enter the first if statement, although employeeIdIS is equal employeeNumber[0].

+5
source share
7 answers

Do not compare strings with ==.

Use

if (string1.equals("other")) {
    // they match
}
+12
source

Compare strings like

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

}
+6
source

== , firststring.equals(secondstring),

+3

equals()

if(employeeId.equals(employeeNumber[i])){}
+2

, , , :

if(employeeId == employeeNumber[i])

2 , ==. equals() equalsIgnoreCase(). == , .. employeeId employeeNumber, . , equals(). equalsIgnoreCase() . == , int, long ..

+2

,   String1.equals(String2);

+2

"==" , . equals() , .

0

All Articles