0

Possible Duplicate:
How do I compare strings in Java?

I read some records from a csv file and store them into a string type array, then save each field in proper variable after convert them. In a part of code I have to compare a field in array with one of those variables with string type. both these arrays filled from a csv file.

            int count=0;
            String name=resourceArray[i][j+1] ;
            while(machineArray[count][0]==name)
            {

                machineID=Integer.parseInt(machineArray[count][1]);
                machinePe=Integer.parseInt(machineArray[count][2]);
                count++;
             }

the problem is 'while' condition never become true. I debug it and I'm sure machineArray[0][0] and 'name' have same value.

Community
  • 1
  • 1

3 Answers3

0

Try changing your condition from machineArray[count][0]==name to machineArray[count][0].equals(name).

Russell Gutierrez
  • 1,372
  • 8
  • 19
0

Essentially you are comparing pointers to two String objects by using == instead of comparing Strings.

To compare Strings you have to use Strings equals method:

machineArray[count][0].equals(name)
dngfng
  • 1,923
  • 17
  • 34
-1

Strings do not always have the same identity (although it is common). Try:

machineArray[count][0].equals(name)
David Grant
  • 13,929
  • 3
  • 57
  • 63