Operator && cannot be applied to 'boolean', 'int

Scott Adamson :

I am trying to override the equals method but am faced with a 'Operator && cannot be applied to 'boolean','int'' error, which seems to be caused by the last line. My belief is that pages == otherEntry.pages returns an int, but why?


        @Override
         public boolean equals (Object that) {
            if (this == that) return true;
            if (!(that instanceof BookEntry)) return false;

            BookEntry otherEntry = (BookEntry) that;

            return title.equals(otherEntry.title) &&
                    Arrays.equals(authors, otherEntry.authors) &&
                    Float.compare(otherEntry.rating, rating) &&
                    ISBN.equals(otherEntry.ISBN) &&
                    pages == otherEntry.pages;
        }
Eran :

Float.compare(otherEntry.rating, rating) returns an int.

Change it to Float.compare(otherEntry.rating, rating) == 0 if you want to determine if the two floats are equal according to the ordering defined by Float.compare().

The return statement would become

        return title.equals(otherEntry.title) &&
                Arrays.equals(authors, otherEntry.authors) &&
                Float.compare(otherEntry.rating, rating) == 0 &&
                ISBN.equals(otherEntry.ISBN) &&
                pages == otherEntry.pages;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=356026&siteId=1