15,964,177 members
See more:
In this method i am returning the nearest assistant based on the distance between the locations. However, the way this works i could end up with a negative number for the distance if the two operands of the subtraction happen to be one way around rather than the other. And i need the positive equivalent, but im not sure how to code this.

What I have tried:

Java
```<pre>public Assistant findNearestAvailable(int location)
{
Iterator<Assistant> it = assistants.iterator();
Assistant nearestAssistant = null;
int nearestDistance = Integer.MAX_VALUE;
int nearestID = Integer.MAX_VALUE;

while(it.hasNext()) {
Assistant assistant = it.next();
if(assistant.isAvailable()) {
int distance = location - assistant.getLocation();
if(distance < nearestDistance){
nearestDistance = distance;
nearestAssistant = assistant;
}
else if(){

}

else if(distance == nearestDistance){
if(identity < nearestID){
nearestID = identity;
nearestAssistant = assistant;
}
}
}
}
return nearestAssistant;
}```
Posted
Updated 21-Nov-22 7:39am
v2

## Solution 1

Look at the Java - abs() Method[^]

Freddie Francis 21-Nov-22 12:29pm
what am i doing wrong here. It tells me that theres no suitable method found for abs:

if(distance < nearestDistance){
nearestDistance = distance(Math.abs());
nearestAssistant = assistant;
}
OriginalGriff 21-Nov-22 12:52pm

But ... did you `import java.lang.Math`

## Solution 2

Replace
Quote:
int distance = location - assistant.getLocation();
with
Java
`int distance = Math.abs(location - assistant.getLocation());`