-2

I'm finishing up a project for school, but I have to create a method that returns a value -1, 0, or 1 based on the alphabetical order of the first letter of name objects. I'm confused on why I keep getting an error asking me for a return value, perhaps I can't see what I'm missing but any help would be appreciated (I'll probably see a teaching assistant tomorrow or the day after).

public int compareTo(Name nameObject) {
    if (middleName.equals(null)) {
        if (getLastName().charAt(0) < nameObject.getLastName().charAt(0)) {
            return -1;
        } else if (getLastName().charAt(0) > nameObject.getLastName().charAt(0)) {
            return 1;
        } else if (getLastName().charAt(0) == (nameObject.getLastName().charAt(0))) {
            if (getFirstName().charAt(0) < nameObject.getFirstName().charAt(0)) {
                return -1;
            } else if (getFirstName().charAt(0) == nameObject.getFirstName().charAt(0)) {
                return 0;
            } else if (getFirstName().charAt(0) > nameObject.getFirstName().charAt(0)) {
                return 1;
            }
        }
    } else {
        if (getLastName().charAt(0) < nameObject.getLastName().charAt(0)) {
            return -1;
        } else if (getLastName().charAt(0) > nameObject.getLastName().charAt(0)) {
            return 1;
        } else if (getLastName().charAt(0) == (nameObject.getLastName().charAt(0))) {
            if (getFirstName().charAt(0) < nameObject.getFirstName().charAt(0)) {
                return -1;
            } else if (getFirstName().charAt(0) > nameObject.getFirstName().charAt(0)) {
                return 1;
            } else if (getFirstName().charAt(0) == nameObject.getFirstName().charAt(0)) {
                if (getMiddleName().charAt(0) < nameObject.getMiddleName().charAt(0)) {
                    return -1;
                } else if (getMiddleName().charAt(0) == nameObject.getMiddleName().charAt(0)) {
                    return 0;
                } else if (getMiddleName().charAt(0) > nameObject.getMiddleName().charAt(0)) {
                    return 1;
                }
            }
        }
    }
}
derpt34
  • 39
  • 5

1 Answers1

0

Your "else if" line(s) are dangling. E.g.,

 } else if (getLastName().charAt(0) == (nameObject.getLastName().charAt(0))) {

Any "else if" needs to follow with an "else" -- or just a return value -- to ensure something is returned.

Keith
  • 3,079
  • 2
  • 17
  • 26