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

  • Context: MHB 
  • Thread starter Thread starter smilesofmiles
  • Start date Start date
  • Tags Tags
    Average Program
Click For Summary
SUMMARY

The discussion focuses on modifying a Java program to accept an unknown number of user inputs for averaging. The original implementation incorrectly initialized an array with a fixed size, resulting in zeros in the output. The solution involves using the Scanner class to read a single line of input, splitting it into an array of strings, and then converting those strings to doubles using Double.parseDouble(). This approach allows for dynamic array sizing based on user input.

PREREQUISITES
  • Java programming fundamentals
  • Understanding of arrays in Java
  • Familiarity with the Scanner class for user input
  • Basic knowledge of string manipulation in Java
NEXT STEPS
  • Explore Java's ArrayList for dynamic array management
  • Learn about exception handling in Java to manage input errors
  • Investigate Java streams for more advanced data processing
  • Study algorithms for calculating averages and other statistical measures
USEFUL FOR

Java developers, programming students, and anyone interested in improving their skills in handling user input and data processing in Java applications.

smilesofmiles
Messages
18
Reaction score
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: 109
Technology news on Phys.org
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;
    }
 

Similar threads

Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
8
Views
2K
Replies
1
Views
8K
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K