3

I don't see anything that I am doing wrong, but NetBeans gives me the following error:

incomparable types
required: boolean
found: java.lang.Object


public int compareTo(Object obj)  {
    if( obj instaceof Employee){
       Employee employee = (Employee) obj;
       if(this.weekly_earnings > employee.weekly_earnings)
           return 1;
       else if(this.weekly_earnings == employee.weekly_earnings)
           return 0;
       else
           return -1;
    }
    else{
        System.out.println("Error");
    }
}
user69514
  • 26,935
  • 59
  • 154
  • 188
  • 2
    And it doesn't return anything from the error case (should throw `ClassCastException`, actually should use generics). – Tom Hawtin - tackline Mar 05 '10 at 00:29
  • 1
    I would also like to add that it would probably be better if you just returned (this.weekly_earnings - employee.weekly_earnings), saves on all the if statements. – DaveJohnston Mar 05 '10 at 00:32

1 Answers1

8

It's spelled instanceof.

Also, as Tom Hawtin mentioned in a comment, if you're using Java 1.5 or later you can write compareTo(Employee emp) to avoid using instanceof at all. There's a thorough section on writing Comparable types in the Object Ordering Java tutorial.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880