Taylor
Taylor

Reputation: 23

Declaring a boolean method in java

Sorry if this is a primitive topic, I have a professor that doesn't speak english and I'm rather lost.

I'm trying to declare a method that checks if a triangle is equilateral. It keeps telling me that I'm comparing a boolean to an int in my if statement. side1, side2, and side3 are all int types.

public boolean is_equilateral(){
    if (side1 == side2 == side3){
        return true;
    }
    return false;
}

Thanks for the help ahead of time!

Upvotes: 1

Views: 508

Answers (4)

Maddy
Maddy

Reputation: 167

Use

if((side1 == side2) && (side2 == side3))

instead of

if(side1 == side2 == side3)

Upvotes: 1

siom
siom

Reputation: 1807

The expression side1 == side2 evaluates to boolean, thus you cannot compare it to another int. But you can do:

if((side1 == side2) && (side2 == side3)) {
...
}

Upvotes: 4

Jeroen Vannevel
Jeroen Vannevel

Reputation: 44439

In your comparison if (side1 == side2 == side3) it will first compare side1 == side2, resulting in a boolean.

Afterwards it will compare the first result (boolean) with the last element (int), thus giving an error. You can't compare a boolean to an int.

Upvotes: 6

Jim Garrison
Jim Garrison

Reputation: 86774

Use

side1 == side2 && side1 == side3

instead.

Upvotes: 3

Related Questions