0

I am a beginner with Java (only about a month into Java). I need to write a program that reads 5 integers and then prints out the largest. I have done it, however I was told there is a more efficient way of doing it,how can i achieve that? Any advice will be appreciated!

   Scanner input = new Scanner(System.in);

   int A = input.nextInt();
   int B = input.nextInt();
   int C = input.nextInt();
   int D = input.nextInt();
   int E = input.nextInt();

   if (A > B && A > C && A > D && A > E)
   {
       System.out.println(A);
   }
   if (B > A && B > C && B > D && B > E)
   {
       System.out.println(B);
   }
   if (C > A && C > B && C > D && C > E)
   {
        System.out.println(C);
   }
   if (D > A && D > B && D > C && D > E)
   {
        System.out.println(D);
   }
   if (E > A && E > B && E > C && E > D)
   {
        System.out.println(E);
   }
Sunshine
  • 3
  • 2
  • 3
    put them in a list sort and get the first –  Nov 01 '17 at 12:07
  • 3
    Use an array to hold the inputs and a loop to find the highest number. – Eran Nov 01 '17 at 12:07
  • 1
    A few keywords to Google, to put you on the right track. "java arrays", "java loops", and maybe "java arraylist" for a little later. – Strikegently Nov 01 '17 at 12:09
  • 4
    Possible duplicate of [smallest and largest of the inputs](https://stackoverflow.com/questions/15328779/smallest-and-largest-of-the-inputs) or if you want to use an array: https://stackoverflow.com/questions/1484347/finding-the-max-min-value-in-an-array-of-primitives-using-java – Tom Nov 01 '17 at 12:10
  • Discard all inputs that are not higher than the previous one. – daniu Nov 01 '17 at 12:10
  • 2
    [`IntStream.of(A, B, C, D, E).max().getAsInt()`](https://docs.oracle.com/javase/8/docs/api/java/util/stream/IntStream.html#max--) – Elliott Frisch Nov 01 '17 at 12:15

7 Answers7

6
int largest = Integer.MIN_VALUE;
int value = 0;
for (int i = 0; i <= 4; i++) {
    value = input.nextInt();
    if (value > largest) {
        largest = value;
    }
}
System.out.println(largest);
  1. Input 5 values
  2. On each input, compare it with the largest variable.
  3. Efficient because it doesn't have any additional overhead of sorting or collection api
Danyal Sandeelo
  • 12,196
  • 10
  • 47
  • 78
  • @Tom care to share why? – Danyal Sandeelo Nov 01 '17 at 12:12
  • Does not work if all inputted ints are negative. Better to initialize with `largest = Integer.MIN_VALUE;` – Stefan Nov 01 '17 at 12:13
  • Logically you mean, I see, i thought you are talking about some design issue in java. Yes, makes sense. Correct. – Danyal Sandeelo Nov 01 '17 at 12:13
  • @Stefan fixed already. – Danyal Sandeelo Nov 01 '17 at 12:14
  • 1
    @DanyalSandeelo, Are you serious? this is even worse... you are using an uninitialized value! You should initialize it to minimal possible value of int... – Daniel Trugman Nov 01 '17 at 12:15
  • 1
    Well, since the answer has been fixed, I think this is of now the best answer, as it doesn't save all the values unncesserarily. – Tobias Brösamle Nov 01 '17 at 12:21
  • @DanielTrugman, you are right, it won't let me compile the code if I don't initialize it. – Danyal Sandeelo Nov 01 '17 at 12:23
  • @TobiasBrösamle, saving 4 ints bothers you? – Daniel Trugman Nov 01 '17 at 12:28
  • @DanielTrugman obviously taking more space and this one is not using any additional data structure. Sorting is overhead, collection api is again an overhead. – Danyal Sandeelo Nov 01 '17 at 12:30
  • @DanielTrugman Yes, doing unnecessary stuff bothers me. As Danyal said, it's overhead that can be avoided. As the OP is a beginner, he/she should learn to avoid unnecessary stuff from the beginning. Next time, it's not only 5 ints, but 1,000,000 complex objects. And then there will be the question "Why is my code so slow?". – Tobias Brösamle Nov 01 '17 at 15:12
  • @TobiasBrösamle, I see your point, bot on the other hand, tomorrow he will want the 5 biggest numbers, and he will open a new question, because this solution won't work. Moreover, I don't support educating code monkeys, a programmer should know how to use his tools. – Daniel Trugman Nov 01 '17 at 15:17
0

Instead of comparing them, simply add them to a SortedSet:

SortedSet set = new TreeSet();

set.add(input.nextInt());
[..]
set.add(input.nextInt());

System.out.println(set.first()); // Smallest
System.out.println(set.last()); // Largest

And then first() will be the smallest one and last() the largest one.

Daniel Trugman
  • 8,186
  • 20
  • 41
0

Take an array of size 5.

Scanner sc = new Scanner(System.in);

int a[] = new int[5];

    for(int i=0;i<5;i++)
    a[i] = sc.nextInt();

Arrays.sort(a);

System.out.println(a[4]);
Karan
  • 448
  • 4
  • 9
0

You can use Collections from java.util. Here is an example:

Collections.max(Arrays.asList(10,9,6,4,-1,7,19,65,0,3));
0

Create an Array

    int a[] = new int[n];
    for(int i = 0; i < n; i++){
    a[i] = s.nextInt();
    }

New sort it

     max = a[0];
     for(int i = 0; i < n; i++){
        if(max < a[i])
        {
            max = a[i];
        }
    }
    System.out.println("Maximum value:"+max);
}
}
Cp.Jack
  • 11
  • 1
0

You can do something like this. Catch the input by placing it inside array with size of 5.

public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
int[] numbers = new int[5];
int largest = Integer.MIN_VALUE;

for (int i = 0; i < numbers.length; i++) {
    System.out.println("Enter numbers");
    numbers[i] = input.nextInt();
}

for(int i =0;i<numbers.length;i++) {
    if(numbers[i] > largest) {
            largest = numbers[i];
        }
    }

    System.out.println("Largest : " +largest);
}
RyeRai
  • 118
  • 1
  • 2
  • 10
0

A simple but not elegant way is to use Math.max(int a, int b) chained.

int max = Math.max(a, b); // returns the max of the two given numbers
int max = Math.max(a, Math.max(b, c)); // maximum of a, b, c 

so in your case:

int A = input.nextInt();
int B = input.nextInt();
int C = input.nextInt();
int D = input.nextInt();
int E = input.nextInt();

int max = Math.max(A, Math.max(B, Math.max(C, Math.max(D, E))));
System.out.println(max);

Alternatively you can create a method to compare variable length of inputs

    public static int getMax(int a, int... rest) {
        int maximum = a;
        for (int value : rest) {
            maximum = Math.max(maximum, value);
        }
        return maximum;
    }

and call this method in your main

int max = getMax(A,B,C,D,E ...);
Eritrean
  • 15,851
  • 3
  • 22
  • 28