Method getFirst returns the String that comes first (is the smallest) among its parameters a, b, and c, each of which is not null.

// precondition: a != null && b != null && c != null
public static String getFirst(String a, String b, String c)
{
    String first = a;

    if(b.compareTo(first) < 0)
        first = b;
    
    if(c.compareTo(first) < 0)
        first = c;
    
    return first;
}

The same standard algorithm is used to find the min/max of a fixed number of values. Set the min/max to the first thing that could be the min/max. Check each additional value against the min/max so far. Update the min/max when a new min/max is found. The conditional statements are intentionally independent. (The second statement is NOT else if.)

A common mistake when finding the min/max of a fixed number of values is comparing each value to each other value. (ex: a to b, b to c, and a to c). Although it is possible to do so correctly, it is more complex, more error prone, and does not scale well to a larger number of values.

If there are more than 4 values, consider adding them to an array or ArrayList and using a loop.

For more information on String comparisons, see compareTo on the AP CS A Exam.

Comments

Comment on Finding the minimum or maximum