Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8520887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:39:26+00:00 2026-06-11T06:39:26+00:00

the code below compiles perfectly for 5 integers entered by the user. what i

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T06:39:28+00:00Added an answer on June 11, 2026 at 6:39 am

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code below demonstrates a behavior of gcc 4.6.2 that I can't account for.
I can't get the code below to compile (see errors). Advice on correction would
It is a compiler error or runtime error? The code below can be compiled!
How can this code compile? The code below in the operator int CAN access
The below code compiles with gcc v4.3.3 and the templated child class seems to
I have the demonstrator code below that fails to compile with the error: Occurs
The code below compiles, but immediately crashes for reasons obvious to others, but not
The code below compiles fine with VS2010 but fails to compile with gcc 4.6.1:
I'm trying to add basic authorization to my request header. The code below compiles
I cannot understand why the code below compiles. public void Overflow() { Int16 s

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.