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: 98
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;
    }
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top