Go Premium for a chance to win a PS4. Enter to Win

x
Solved

# finding the lowest of 4 values

Posted on 2009-04-06
Medium Priority
184 Views
If I have values A, B, C and D
and I wish to find which has the lowest value.

What would be the best way to do this?
0
Question by:jtiernan2008

LVL 92

Accepted Solution

objects earned 1200 total points
ID: 24082368
You can use Math.min()

min = Math.min(A, Math.min(B, ....

or use if

int min = Integer.MAX_VALUE;
if (A
0

LVL 1

Author Comment

ID: 24082433
so this would be

min=Math.min(A,B,C,D);
0

LVL 20

Expert Comment

ID: 24082450
No, you can have only two parameters so as objects showed above, you need to nest them
0

LVL 92

Expert Comment

ID: 24082462
min only takes two params so you need to call it muliple times

0

LVL 4

Assisted Solution

aman123_123 earned 800 total points
ID: 24083879
Here are two ways to achieve the same.

Using list and sort it:
List<Integer> list = new ArrayList<Integer>();
Collections.sort(list);
for(int a:list)
System.out.println(a);

Using Sorted set i.e tree set
Set<Integer> set = new TreeSet<Integer>();

for(int a:set)
System.out.println(a);

First element of these collections will always be the minimum.
0

LVL 1

Author Closing Comment

ID: 31567283
thanks a million
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
###### Suggested Courses
Course of the Month10 days, 15 hours left to enroll