5

I'm learning basic Java right now and have a problem with my code that I can't figure out. It's basically what the title says. My Java compiler is telling me that there's an error with my custom compareTo method, saying that it needs to return an int. The problem is, as far as I can tell, it IS returning an int. Yet it's still giving me an error. Could someone please point out in my code what's wrong? And also I have already implemented Comparable in my class. Here's my method:

public int compareTo(Homework other) {
    if (getDaysLate() < other.getDaysLate()) {
        return -1;
    } else if ((dateSubmitted == other.dateSubmitted)
            && (files.compareTo(other.files) == -1)) {
        return -1;
    } else if ((dateSubmitted == other.dateSubmitted)
            && (files == other.files)) {
        if (name.compareTo(other.name) == -1) {
            return -1;
        } else if (name.compareTo(other.name) == 1) {
            return 1;
        } else if (name.compareTo(other.name) == 0) {
            return 0;
        }
    } else {
        return 0;
    }
}
Perception
  • 79,279
  • 19
  • 185
  • 195

5 Answers5

2

There is a path in the third else that does't return anything.

 else if ((dateSubmitted == other.dateSubmitted) && (files == other.files)) {
    if (name.compareTo(other.name) == -1) {
      return -1;
    }
    else if (name.compareTo(other.name) == 1) {
      return 1;
    }
    else if (name.compareTo(other.name) == 0) {
      return 0;
    } else return ...
}

BTW, I'm not sure if I'm following the logic of your implementation, since it seems that you are returning 0 if dateSubmitted != other.dateSubmitted. compareTo should also be anti-symetric (i.e. sgn(x.compareTo(y)) == -sgn(y.compareTo(x))), but your implementation is not.

Javier
  • 12,100
  • 5
  • 46
  • 57
  • @TedHopp Right (thanks, fixed). I also corrected that antisymmetry is only required for the **sign** of the result (both are zero, or one is positive and the other is negative). The absolute value doesn't matter. – Javier Mar 08 '13 at 06:51
1

You are missing an else after this branch:

else if (name.compareTo(other.name) == 0) {
    return 0;
}

If the test fails (compareTo doesn't return 0) the method would have to exit without a return value, which is illegal in Java.

Also, compareTo may return any integer value, not only 0, 1, and -1.

Joni
  • 108,737
  • 14
  • 143
  • 193
1

How can you be sure (with all these if and else) that you are always returning an int? It does not seem so obvious to me and aparently the compiler agrees with me too.

One way to solve this (probably not the best one) is to add a return -1; //or whatever value at the end of your function.

Burkhard
  • 14,596
  • 22
  • 87
  • 108
1

In your second else-if statement you have a code path that may not return anything. You say:

else if ((dateSubmitted == other.dateSubmitted) && (files == other.files)) {  
    if (name.compareTo(other.name) == -1) {  
        return -1;  
    }  
    else if (name.compareTo(other.name) == 1) {  
        return 1;  
    }  
    else if (name.compareTo(other.name) == 0) {  
       return 0;  
    }    

, but what if none of those else if's are true? Try changing the last else-if in your second else-if statement to else.

astidham2003
  • 966
  • 1
  • 11
  • 33
0

The method should return appropriate value on all code flow paths; in other words, on all conditions when the method returns. In the following if block it does not return on one path that I've marked.

   else if ((dateSubmitted == other.dateSubmitted) && (files == other.files)) {
        if (name.compareTo(other.name) == -1) {
        return -1;
        }
        else if (name.compareTo(other.name) == 1) {
        return 1;
        }
        else if (name.compareTo(other.name) == 0) {
        return 0;
        }
        // It should return something here, if none of the above "if" statements match.
        // Or one of the above "else if" should be changed to "else"
    }
Kshitij
  • 361
  • 1
  • 9