user400377
user400377

Reputation:

String comparison passing through if statement when it shouldn't

I'm comparing values of an excel sheet to record values returned from a database and one record is passing through the if statement when it should fail the if statement.

The if statement looks like this:

if (record.value.equals(cellVal) == false)

    {
        record.value = cellVal
        record.modifyUser = userId
        //dataService.updateManualEntry(record)
        println "UPDATING ${record.value.equals(cellVal)}"
        println "record value: ${record.value}"                                 
        updatedCount++
        }else{
            println "NOT UPDATING [ [ ${record.value.length()} ]  + [${cellVal.length()}]"

        }

            }

The println shows that the value of println "UPDATING ${record.value.equals(cellVal)}" evaluates to be true, in which case I don't understand why it is passing through the if statement. In addition the length of the string is 0.

Can I get a second pair of eyes and figure out why a true value would get through this if statement?

Upvotes: 0

Views: 1192

Answers (5)

C_Rance
C_Rance

Reputation: 661

if ->record.value.equals(cellVal) give u false u compare (false)==false, naturally false is always = false, hence it will pass through

2nd, look at ur code,

record.value = cellVal

record.modifyUser = userId

//dataService.updateManualEntry(record)

println "UPDATING ${record.value.equals(cellVal)}"

U assign record.value = cellVal before u do a print. Of cuz it will print true.

Upvotes: 0

Tushar Tarkas
Tushar Tarkas

Reputation: 1622

the println is showing true becoz of the 1st line in your if statement

record.value = cellVal

try printing the same value before if statement.

Upvotes: 2

Behrang Saeedzadeh
Behrang Saeedzadeh

Reputation: 47971

Here:

record.value = cellVal

You have set record.value to cellVal inside the if block. That's why the println returns true.

Upvotes: 3

Carl Manaster
Carl Manaster

Reputation: 40356

Your printlns happen after you have changed the value to match.

Upvotes: 4

msw
msw

Reputation: 43527

(true == false) ⇒ false

Upvotes: 2

Related Questions