Solved

finding the lowest of 4 values

Posted on 2009-04-06
7
178 Views
Last Modified: 2012-05-06
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
Comment
Question by:jtiernan2008
7 Comments
 
LVL 92

Accepted Solution

by:
objects earned 300 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

by:jtiernan2008
ID: 24082433
so this would be

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

Expert Comment

by:Venabili
ID: 24082450
No, you can have only two parameters so as objects showed above, you need to nest them
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

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

0
 
LVL 4

Assisted Solution

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

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

Using Sorted set i.e tree set
        Set<Integer> set = new TreeSet<Integer>();
        set.add(100);
        set.add(10);
        set.add(15);
       
        for(int a:set)
            System.out.println(a);


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

Author Closing Comment

by:jtiernan2008
ID: 31567283
thanks a million
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse apache tomcat admin console 52 156
What's wrong with this code? 4 36
collection output issue 9 66
DTD and JAVA versions 1 33
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question