Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: find the largest number in a list of a number of integers that the user has set

Tags:

java

loops

list

the code below compiles perfectly for 5 integers entered by the user.
what i want to do is alter my code so that i can as the user how many numbers the user wants in the list. then the program will ask the user to enter that many integers and then the program will find the largest of those integers. can anyone help me alter the code below to fit these new standards?

{
    int integer1 = 
    Integer.parseInt(JOptionPane.showInputDialog( "Enter an integer:" ));
    int integer2 = 
    Integer.parseInt(JOptionPane.showInputDialog( "Enter an integer:" ));
    int integer3 = 
    Integer.parseInt(JOptionPane.showInputDialog( "Enter an integer:" ));
    int integer4 = 
    Integer.parseInt(JOptionPane.showInputDialog( "Enter an integer:" ));
    int integer5 = 
    Integer.parseInt(JOptionPane.showInputDialog( "Enter an integer:" ));   

    if (integer1 > integer2 && integer1 > integer3 && integer1 > integer4 && integer1 > integer5)
        JOptionPane.showMessageDialog(null, "The largest number is: " + integer1);
    if (integer2 > integer1 && integer2 > integer3 && integer2 > integer4 && integer2 > integer5)
        JOptionPane.showMessageDialog(null, "The largest number is: " + integer2);
    if (integer3 > integer2 && integer3 > integer1 && integer3 > integer4 && integer3 > integer5)
        JOptionPane.showMessageDialog(null, "The largest number is: " + integer3);
    if (integer4 > integer1 && integer4 > integer3 && integer4 > integer2 && integer4 > integer5)
        JOptionPane.showMessageDialog(null, "The largest number is: " + integer4);
    if (integer5 > integer1 && integer5 > integer3 && integer5 > integer4 && integer5 > integer2)
        JOptionPane.showMessageDialog(null, "The largest number is: " + integer5);
 }

i first want to prompt the user with

how many integers do you want in your list?

then i want to say

enter an integer: 

as many times as the user said he wanted.

like image 577
user1556084 Avatar asked Nov 28 '25 06:11

user1556084


2 Answers

The key here is to use Collections.max.

Returns the maximum element of the given collection, according to the natural ordering of its elements.

The natural ordering for Integer is from least-to-greatest, i.e. ascending. This makes it perfect to use here.

int largest = Collections.max(Arrays.asList(integer1, integer2, integer3,
    integer4, integer5));

Alternatively, you could just build the List using a loop instead. See below for code that prompts the user to input the number of integers to enter.

int n = Integer.parseInt(
    JOptionPane.showInputDialog("How many integers do you want in your list?"));
List<Integer> inputs = new ArrayList<Integer>(n);
for (int i = 0; i < n; ++i) {
  inputs.add(Integer.parseInt(
      JOptionPane.showInputDialog("Enter an integer:")));
}
int largest = Collections.max(inputs);
JOptionPane.showMessageDialog(null, "The largest number is: " + largest);
like image 110
obataku Avatar answered Nov 30 '25 21:11

obataku


You know how to obtain an integer from the user, so the first part of the new requirements is easy:

int n = Integer.parseInt(JOptionPane.showInputDialog(
    "how many integers do you want in your list?" ));

Then you will need to loop and collect that many integers. You don't need to store them, since you are just looking for the largest one.

int maxSoFar = Integer.MIN_VALUE;
for (int i = 0; i < n; ++i) {
    int integer = 
        Integer.parseInt(JOptionPane.showInputDialog( "enter an integer:" ));
    if (integer > maxSoFar) {
        maxSoFar = integer;
    }    
}
like image 21
Ted Hopp Avatar answered Nov 30 '25 21:11

Ted Hopp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!