compareTo() method throwing "This method must return int" error despite returning int in method

Viewed 177

I'm working on an assignment for my computer science course requiring me to implement a Comparable interface.

We haven't discussed the interface at any sort of length except just being told it compares two objects and returns less than, greater than and equal to, but literally that's about it, which is frustrating.

I intend to do more research on it, but for now I am finding I'm confused as to why my implementation of the compareTo() method isn't working.

Eclipse is giving me an error that compareTo() must return an int, but if you notice, I am returning an integer value. So what might be the issue?

public int compareTo(Task taskToCompare) {
    if(this.complete && taskToCompare.isComplete()) {
        if(this.priority == taskToCompare.getPriority()) {
            return 0;
        }
        else if(this.priority < taskToCompare.getPriority()){
            return -1;
        }
        else if(this.priority > taskToCompare.getPriority()) {
            return 1;
        }
    } else if(this.complete == true && taskToCompare.isComplete() == false) {
        return -1;
    } else if(this.complete == false && taskToCompare.isComplete() == true) {
        return 1;
    }
}
3 Answers

If the return type is int, you will have to return an int or throw an exception. Just exiting the method without a return will lead to a compiler error.

If you have a if-else-if condition, there may be a case where none of the blocks is called. You therefore should create an else statement with a return.

Also, the result of isComplete() and taskToCompare.getPriority() may change if you call the method multiple times. The compiler doesn't know if your logic prevents that.

For example, this is the case if complete is false and isComplete() also returns false. As before, the compiler doesn't know if your logic prevents that.

I think you want something like:

public int compareTo(Task taskToCompare) {
    if(this.complete && taskToCompare.isComplete()) {
        if(this.priority == taskToCompare.getPriority()) {
            return 0;
        }
        else if(this.priority < taskToCompare.getPriority()){
            return -1;
        }
        else{
            return 1;
        }
    } else if(this.complete == true && taskToCompare.isComplete() == false) {
        return -1;
    } else if(this.complete == false && taskToCompare.isComplete() == true) {
        return 1;
    }else{
        return 0;
    }
}

What if this.complete == false and taskToCompare.isComplete() == false?

The compiler is complaining because you haven't covered every case.

more compact version:

public int compareTo(Task taskToCompare) {
        int completeCompare = (this.complete == taskToCompare.complete) ? 0 : (this.complete  ? 1 : -1);
        if(completeCompare==0) {
            return  this.priority-taskToCompare.getPriority();
        }
        return completeCompare;
    }
Related