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 8123651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:14:47+00:00 2026-06-06T06:14:47+00:00

Im trying to make a program that retrieves an endless amount of numbers that

  • 0

I”m trying to make a program that retrieves an endless amount of numbers that user inputs, and then it tells you how many numbers that you inputted, the sum of all the numbers, and then the average of the numbers. Here is the code I have so far. I don’t know why it does not work. I get no errors, but it just does not get a valid sum or average.

import javax.swing.*;

public class SumAverage {
    public static float sum;
    public static float averageCalculator;
    public static float average;
    public static void main(String[]args) {
        float numbers[] = null;
        String userInput = JOptionPane.showInputDialog(null, "Ready to begin?");
        if(userInput.equalsIgnoreCase("no"))
        {
            System.exit(0);
        }
        for(int i = 0; i != -2; i++)
        {
            numbers = new float[i + 1];
            userInput = JOptionPane.showInputDialog(null, "Input any number. Input * to exit");
            if(userInput.length() == 0 || userInput.equals("*") || userInput.equals(null))
            {
                break;
            }
            else 
            {
                numbers[i] = Float.parseFloat(userInput);   
            }
        }
        for (int i = 0; i < numbers.length; i++)
        {
            sum += numbers[i];
        }

        average = sum / numbers.length;

        JOptionPane.showMessageDialog(null, "The sum of all your numbers is " + sum + ". The average is " + average + ". You entered a total of " + numbers.length + " numbers.");
    }
}
  • 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-06T06:14:49+00:00Added an answer on June 6, 2026 at 6:14 am

    The problem is in this line:

     numbers = new float[i + 1];
    

    You are creating a new array, but you aren’t copying the values from the previous array assigned to numbers into it.

    You can fix this in two ways:

    • copy the values using System.arraycopy() (you’ll need to use a new variable to make the call then assign it to numbers)
    • Don’t use arrays! Use a List<Float> instead, which automatically grows in size

    In general, arrays are to be avoided, especially for “application logic”. Try to always use collections – they have many powerful and convenient methods.

    If you wanted to store the numbers for later use, try making your code look like this:

    List<Float> numbers = new ArrayList<Float>();
    ...
    numbers.add(Float.parseFloat(userInput));
    ...
    for (float n : numbers) {
        sum += n;
    }
    average = sum / numbers.size();  // Note: Don't even need a count variable
    

    And finally, if you don’t need to store the numbers, just keep a running sum and count and avoid any kind of number storage.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a program that loads and image and then splits
I am trying to make a program that Encrypts data using AES, then encrypts
So i'm trying to make a program that can find how many pixels of
I'm trying to make a program that gets two input numbers, multiplies them (storing
I'm trying to make a Python program that retrieves only the body text of
I'm trying to make a program that can be hosted by many peoples, like
I'm trying to make a basic program that allows a user to create a
I am trying to make a program that will allow a user to view
I'm trying to make a program that prompts the user for 2 items -
I'm trying to make a program that will open a directory, then use regular

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.