Is there a faster way to work out this logic?

The purpose of this assignment was the find the smallest and largest of the integer(User inputs 5 entries)

I am a beginner Java programmer with no prior experience. Is there an easier way to do this without a ton of if/else statements?

Forgive for the dirty code.

``````    //Scanner Object
Scanner input = new Scanner(System.in);

int num1, num2, num3, num4, num5;
int large;
int small;

System.out.print("Enter Num1: ");
num1 = input.nextInt();

System.out.print("Enter Num2: ");
num2 = input.nextInt();

System.out.print("Enter Num3: ");
num3 = input.nextInt();

System.out.print("Enter Num4: ");
num4 = input.nextInt();

System.out.print("Enter Num5: ");
num5 = input.nextInt();

//If/Else statements (SMALL)
if (num1<num2 && num1<num3 && num1<num4 && num1<num5) {
small = num1;

}

else if (num2<num1 && num2<num3 && num2<num4 && num2<num5) {
small = num2;

}

else if (num3<num1 && num3<num2 && num3<num4 && num3<num5) {
small = num3;

}

else if (num4<num1 && num4<num2 && num4<num3 && num4<num5) {
small = num4;

}

else {
small = num5;

}

//Large

if (num1>num2 && num1>num3 && num1>num4 && num1>num5) {
large = num1;
}

else if (num2>num1 && num2>num3 && num2>num4 && num2>num5) {
large = num2;
}

else if (num3>num1 && num3>num2 && num3>num4 && num3>num5) {
large = num3;
}

else if (num4>num1 && num4>num2 && num4>num3 && num4>num5) {
large = num4;
}

else {
large = num5;

}

//Display: Use printf when using a format limiter (%)

System.out.printf("The smallest integer: %d\n", small);
System.out.printf("The largest integer: %d\n", large);

}// end of main

}
``````
• If this is homework, please add the homework tag. – Caleb May 24 '12 at 21:10
• This is absolutely not my homework.Got this from my brothers Android book lying around. I am thinking of diving into Android after i got the java basics down. The code i gave above works but i want to make it smaller. But, i can add the homework tag if thats going to help me. – AppSensei May 24 '12 at 21:16
• Not necessary, Rony John. If it were actual homework, it's best to tag it that way so that someone doesn't just answer the entire question and the OP never learns anything. – Caleb May 24 '12 at 21:41
• @Caleb We have blacklisted [homework], for the reasons discussed here. – yannis May 24 '12 at 22:35
• @YannisRizos Thanks -- didn't realize that Programmers had a different policy than SO on this. – Caleb May 24 '12 at 23:35

You can use not only arrays like Caleb suggested, but also lists and sorting.

It is not the most efficient way, but it is, in my opinion, the fastest one to implement.

``````public static void main(String[] args) {
Scanner input = new Scanner(System.in);
List numbers = new ArrayList<Integer>();

int currentNumber;

for(int i=0; i<5; i++){
System.out.print("Enter Num"+(i+1)+": ");
currentNumber=input.nextInt();
}

Collections.sort(numbers);

System.out.printf("The smallest integer: %d\n", numbers.get(0));
System.out.printf("The largest integer: %d\n", numbers.get(4));

}// end of main
``````

Edit:

An alternative for millions of elements, improved with jk.'s proposal:

``````public static void main(String[] args) {
Scanner input = new Scanner(System.in);

Integer min = Integer.MAX_VALUE;
Integer max = Integer.MIN_VALUE;

int currentNumber;

for(int i=0; i<5; i++){
System.out.print("Enter Num"+(i+1)+": ");
currentNumber=input.nextInt();

max = Math.max(max, currentNumber);
min = Math.min(min, currentNumber);
}

System.out.printf("The smallest integer: %d\n", min);
System.out.printf("The largest integer: %d\n", max);

}// end of main
``````
• Thank You so Much!!!, I have so much to learn. Excited !!! – AppSensei May 24 '12 at 21:26
• Sorting the whole array just to find mininum and maximum seems like a waste of resources to me. – user281377 May 24 '12 at 21:31
• @user281377: it could be, if the array contained a hundred million elements. Otherwise, the shortest code wins. – kevin cline May 24 '12 at 21:33
• @RonyJohn The differences is a factor of log n. That is, the time to scan through all the elements in an array is proportional to n, the number of elements in the array. We denote this O(n). Sorting an array is at least O(n log n) -- that is, the performance will be proportional to the product of the number of elements and the log of the number of elements. For small n, log n is quite small, so no big deal. As n increases, the difference between n and n log n becomes much more significant. – Caleb May 24 '12 at 21:49
• tbh i'd use Math.Max and min rather than coding my own branches. in which case the 'optimized' version will be as brief and arguably more descriptive than the sort version – jk. May 25 '12 at 8:16

There are definitely better ways to do that. Put the values in an array and then scan through the array looking keeping track of the smallest and largest values you find:

``````for (i in valueArray) {
if (i < min)
min = i;
if (i > max)
max = i;
}
``````

I'll leave that into real code up to you.

• Thanks...Life would've been miserable without arrays. Appreciate your help Caleb. – AppSensei May 24 '12 at 21:18
• Back in the '80s when I was < 10 years old, trying my bestest to program in gwbasic, I couldn't figure out arrays... what a shame. `x23 = x24; x24 = x25` gaa; – gahooa May 24 '12 at 21:55

Others have provided good, generalized solutions. I would offer a variant of this which avoids sorting:

``````int largest = Integer.MIN_VALUE;
System.out.print("Enter Num1: ");
num1 = input.nextInt();
largest = Math.max(num1, largest)

System.out.print("Enter Num2: ");
num2 = input.nextInt();
largest = Math.max(num2, largest)

System.out.print("Enter Num3: ");
num3 = input.nextInt();
largest = Math.max(num3, largest)

System.out.print("Enter Num4: ");
num4 = input.nextInt();
largest = Math.max(num4, largest)

System.out.print("Enter Num5: ");
num5 = input.nextInt();
largest = Math.max(num5, largest)
``````

This is basically the same as Caleb's suggestion, but without the enumeration and done along the way.