Comparing multiple integers in IF statement, Java


I am brand new to coding. I’m currently using “Java:How to program” by Deitel, and one of the early exercises has me stumped.

It is asking me to use simple, chapter 1 if statements to compare 5 integers (input by the user) and display both the largest and the smallest. I am limited to the language used in the first chapter, so I made the assumption that I could list int values in the parenthetical after the if statement. Something like:

int firstInt;
int secondInt;
int thirdInt;
int fourthInt;
int fifthInt;

if (first Int> secondIt, thirdInt, fourthInt, fifthInt)

etc. but this is a syntax error. Does anyone know how to compare multiple values simultaneously? A google search proves to be no help since all the answers have some convoluted solution that I don’t understand.

If I try something like this:

     if (one > two)
     System.out.println("%d is the largest number!", one);

     if (one > three)

etc. for all 1 – 5, The issue I run into is when I continue with if statements 1>2,3,4,5 2>1,3,4,5 etc. then there will be 1 true for each integer regardless except for the smallest number, and it will display 4 outputs, when all I’m after is the larges and the smallest.

I’m stumped.


The expression:

firstInt > secondInt, thirdInt, fourthInt, fifthInt

is not valid because Java is a computer language rather than a natural language (like English).

If you want to do it that way, what you need is the and conjunction, with multiple complete conditions (using a,b,c,d,e to minimise the code):

if ((a >= b) && (a >= c) && (a >= d) && (a >= e)) { // a >= b,c,d,e
    System.out.println ("Largest is: " + a);
} else if ((b >= c) && (b >= d) && (b >= e)) {      // b >= c,d,e
    System.out.println ("Largest is: " + b);
} else if ((c >= d) && (c >= e)) {                  // c >= d,e
    System.out.println ("Largest is: " + c);
} else if (d >= e) {                                // d >= e
    System.out.println ("Largest is: " + d);
} else {                                            // e > d
    System.out.println ("Largest is: " + e);

keeping in mind that, once you decide a (or any of the others in later steps) isn’t the largest, you never need to check it again, as one of the others will be larger than it.

Or you can use the facilities of the Java standard Math package, where Math.max(a,b) will give you the largest of the values from a and b:

int largest = Math.max(a,Math.max(b,Math.max(c,Math.max(d,e))));

Here, you define the largest of all five numbers as the maximum of the pair {a, maximum-of-bcde} then use a similar method to work out maximum-of-bcde as the maximum of the set {b, maximum-of-cde} and so on.

Or, in other words, choose the largest from {d,e} and call it x. Then choose the largest from {c,x} and call it y. Then the largest from {b,y} and call it z. Finally the largest from {a,z} and that’s your largest value of the five.

Though, to be honest, if you’re on chapter one of an introductory text, it probably hasn’t covered much in the Java standard libraries yet.

But, if you’re after a method where it’s perhaps easier for a beginner to understand the flow, I don’t think you can go past this one:

int maxOfFive (int a, int b, int c, int d, int e) {
    int largest = a;
    if (b > largest) largest = b;
    if (c > largest) largest = c;
    if (d > largest) largest = d;
    if (e > largest) largest = e;
    return largest;

This is basic selection of the largest value as a person would do it, scanning the list one by one and just remembering which value was the highest, then returning that.

Or, if you haven’t learned about functions yet (and reverting to your original variables), just implement the same code without a function call:

int largest = firstInt;
if (secondInt > largest) largest = secondInt;
if (thirdInt  > largest) largest = thirdInt;
if (fourthInt > largest) largest = fourthInt;
if (fifthInt  > largest) largest = fifthInt;
System.out.println ("Largest is: " + largest);

I believe that’s probably only using stuff you’ve already indicated that you know about (variables, single-condition if statement, and output).

Source: stackoverflow