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
AI Thread 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: 103
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;
    }
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top