Reputation: 75
I want to compare two objects of Manager using three variables, deptNum, firstName and then lastName. I have set up these objects to be compared using a series of if and if-else statements. The error I am receiving is that the valReturn variable may not have been initialized, which I assume means an if statement somewhere along the way is not assigning a value correctly. I'm not sure where I made a mistake.
If there is a more efficient way to set up this comparison, I would be happy to be educated further.
Thank you.
Below is my code:
public class ManagerComparator implements Comparator<Manager> {
public int compare(Manager first, Manager second) {
int valReturn;
if (first.getDeptNum() < second.getDeptNum())
valReturn = -1;
else if (first.getDeptNum() > second.getDeptNum())
valReturn = 1;
else if (first.getDeptNum() == second.getDeptNum()) {
if (first.getFirstName().compareTo(second.getFirstName()) < 0)
valReturn = -1;
else if (first.getFirstName().compareTo(second.getFirstName()) > 0)
valReturn = 1;
else if (first.getFirstName().compareTo(second.getFirstName()) == 0) {
if (first.getLastName().compareTo(second.getLastName()) < 0)
valReturn = -1;
else if (first.getLastName().compareTo(second.getLastName()) > 0)
valReturn = 1;
else if (first.getLastName().compareTo(second.getLastName()) == 0)
valReturn = 0;
}
}
return valReturn;
}
}
Upvotes: 2
Views: 80
Reputation: 4956
With Java 8, you can avoid all this boilerplate code and simply do:
Comparator<Manager> managerComparator = Comparator.comparingInt(Manager::getDeptNum)
.thenComparing(Manager::getFirstName)
.thenComparing(Manager::getLastName);
It's self-explanatory, basically creates a comparator which does the same thing you're trying to do - compare by dept number, then by first name, then by last name.
Upvotes: 2
Reputation: 13970
You're using only else if
. If you follow your logic you can see that all cases will enter one of the statements. However, the compiler doesn't know that.
Simply changing the last else if
in these instances to `else should resolve it.
However, the code in Elliot's post would be a cleaner alternative.
Upvotes: 1
Reputation: 57204
The basic problem you are running into is that the compiler cannot determine that your if ... else if ... else if ...
is exhaustive.
If we look at your last if block it boils down to
if (a() < 0)
valReturn = -1;
else if (a() > 0)
valReturn = 1;
else if (a() == 0)
valReturn = 0;
It is not feasible for the compiler to understand that a()
will return the same value three times meaning that exactly one if condition will be met. The same would happen if you stored a()
inside a temporary variable.
Solution: make the last one an else
instead of else if
.
Upvotes: 3
Reputation: 201537
I would prefer a simple test against 0
with each incremental comparison. Return the result of each compare as you go. Like,
public class ManagerComparator implements Comparator<Manager> {
public int compare(Manager first, Manager second) {
int r = Integer.compare(first.getDeptNum(), second.getDeptNum());
if (r != 0) {
return r;
}
r = first.getFirstName().compareTo(second.getFirstName());
if (r != 0) {
return r;
}
return first.getLastName().compareTo(second.getLastName());
}
}
Upvotes: 8