DeathVenom
DeathVenom

Reputation: 394

Getting error Operator '!=' cannot be applied to 'Boolean' and 'Int' in a very simple if statement

Here is a part of my code in Kotlin:

if (result[0] != 0 and result[2] != 0){
            textView_result.text = result[2].toString() + " " + result[0].toString() + "/" + result[1].toString()
        }

To clarify, result is a List with length 3. This is a very simple code that if the number at index 0 and 2, both are not equal to zero, then the following has to be run.

However, in the condition specified for if, I'm getting this error:

Operator '!=' cannot be applied to 'Boolean' and 'Int'

Am I doing something wrong?

Edit: I tried making new Integer values for result[0], [1] and [2] and replacing them with the integer, but to no avail.

Upvotes: 1

Views: 2568

Answers (2)

Indrajeet Meena
Indrajeet Meena

Reputation: 171

Use && instead of and

if (result[0] != 0 && result[2] != 0){
        textView_result.text = result[2].toString() + " " + result[0].toString() + "/" + result[1].toString()
    }

Because and operator required both side Integer(someInt and someInt) But in your case result[0] != 0 and result[2] != 0 both evaluates to boolean values then and operator is applied which is not permitted

Other way you can cast values and use and operator

Upvotes: 2

Hareth MK
Hareth MK

Reputation: 23

Make Another int called List1 = result[1]; Like that, then try to replace it in the condition instead of result[x]

Upvotes: 1

Related Questions