Solving a Program to Take & Average Unknown Number of User Inputs

In summary: Average(String[] strHolder){ int counter; double userSum = 0; for(counter = 0; counter < strHolder.length; counter++){ userSum = userSum + strHolder[counter]; } return (userSum / counter); } public static void print
  • #1
smilesofmiles
19
0
I want to alter my program so it can take an unknown amount of elements (user inputs). I want the program to automatically count the numbers the user is inputting up until they hit enter to the next line. I think I got the program to keep track of this but now my output is a bunch of zeros in place of the user numbers. I don't know how to fill the array with the user numbers as well as keep track of the array size. I have attached a screenshot of the output.

Please let me know if I've been unclear. Any help, direction or hints will be greatly appreciated. I feel like I'm really close to solving this.

Thank you! :-)

View attachment 5682

Code:
package calcavg;

import java.util.Arrays;
import java.util.Scanner;public class CalcAvg {    public static double [] getArrayNumbers(){
        Scanner input = new Scanner(System.in);
        
        System.out.println("Please enter 5 to 10 numbers all one line separated by spaces.");
        String lines = input.nextLine();
        String[] strs = lines.split(" ");
        double userNumbers[] = new double[strs.length];
        
       
        /* I originally tried this to get the numbers from the user but this asks the user for a number amount to 
        set the size of the array which I don't want. 
 
        System.out.println("Please enter amount of numbers to average.");
        int numbers = input.nextInt( );
        double [ ] userNumbers = new double[numbers];
        System.out.println("Please enter five to ten numbers separated by spaces on one line."); 
        for (int i = 0; i < userNumbers.length; i++) {
            
            userNumbers[i] = input.nextInt( );
            
        }
        */
       
        return userNumbers;
    
    }
    
    public static double calculateAverage(double [ ] userNumbers){
       int counter;
       double userSum = 0;
       
       for(counter = 0; counter < userNumbers.length; counter++){
            userSum = userSum + userNumbers[counter];
        }
        
       return (userSum / counter);
      
    }
  
    public static void printResults(double [] userNumbers, double average){
       System.out.print("The average of the numbers " + Arrays.toString(userNumbers) + " is ");
       System.out.printf("%.2f", average);
       System.out.print(".");
    } 
  
    public static void main(String[] args){
       double userNumbers[] = getArrayNumbers();
       double average = calculateAverage(userNumbers);
       printResults(userNumbers, average);      
    } 
}
 

Attachments

  • Capture.JPG
    Capture.JPG
    6.3 KB · Views: 72
Technology news on Phys.org
  • #2
Never mind, I figured it out. It doesn't stop between 5 and 10 inputs but as long as the user follows the prompt it should be okay.
Code:
public static double [] getArrayNumbers(){
       Scanner input = new Scanner(System.in);
       String numbers = input.nextLine();
       String[] strHolder = numbers.trim().split("\\s+");
       double userNumbers[] = new double[strHolder.length];
       
       for(int counter = 0; counter < strHolder.length; counter++){
           userNumbers[counter] = Double.parseDouble(strHolder[counter]);
        }
       return userNumbers;
    }
 

FAQ: Solving a Program to Take & Average Unknown Number of User Inputs

1. How do I solve a program to take an average of an unknown number of user inputs?

To solve this, you first need to create a loop that will continuously ask for user inputs until the user inputs a specific value to exit the loop (such as a negative number). Then, within the loop, you can keep track of the total sum of the inputs and the number of inputs. Finally, divide the total sum by the number of inputs to get the average.

2. What is the benefit of creating a program to take an average of an unknown number of user inputs?

The main benefit of creating such a program is that it allows for flexibility in the number of inputs. This means that the program can handle any number of inputs, rather than being limited to a set number. It also allows for more efficient and organized data processing.

3. How do I handle errors or invalid inputs when solving this program?

The best way to handle errors or invalid inputs is to use exception handling. This means using try-catch blocks to catch any potential errors and handle them appropriately (such as asking the user to re-enter a valid input).

4. Can I use this program to take the average of non-numerical inputs?

No, this program is designed to take the average of numerical inputs only. If you want to take the average of non-numerical inputs, you may need to convert them to numbers first or create a separate program that can handle non-numerical inputs.

5. Is there a limit to how many inputs this program can handle?

No, there is no limit to the number of inputs this program can handle as long as the computer has enough memory to store the inputs. However, it is important to note that with a large number of inputs, the program may take longer to run and may require more memory and processing power.

Back
Top