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

  • Thread starter Thread starter smilesofmiles
  • Start date Start date
  • Tags Tags
    Average Program
Click For Summary
The discussion centers around modifying a Java program to accept an unspecified number of user inputs and correctly store them in an array. Initially, the program was set up to prompt the user for a fixed number of inputs, leading to issues with outputting zeros instead of the actual numbers. The user sought guidance on how to dynamically fill the array with user inputs while keeping track of the array size. After troubleshooting, the user successfully implemented a solution that allows the program to read a line of input, split it into individual numbers, and convert them into a double array. The final version of the method uses `String.trim()` and `String.split("\\s+")` to handle the input efficiently. The user confirmed that the program now functions as intended, provided the user follows the input instructions correctly.
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: 105
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;
    }
 
Anthropic announced that an inflection point has been reached where the LLM tools are good enough to help or hinder cybersecurity folks. In the most recent case in September 2025, state hackers used Claude in Agentic mode to break into 30+ high-profile companies, of which 17 or so were actually breached before Anthropic shut it down. They mentioned that Clause hallucinated and told the hackers it was more successful than it was...

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