Search code examples
javamethodsnetbeansparametersprogram-entry-point

Calling methods within methods, Calling methods with multiple parameters to main. Mixing String and double


I'm struggling with an assignment. I need help figuring out how to call the right methods within each other and eventually in main. My whole code might need work at this point, what am I doing wrong? I've been stuck on this for a week.. (Guidelines at the bottom) Thanks!

import java.util.Scanner;

public class AverageWithMethods {
static Scanner in = new Scanner(System.in);

  public static void main(String[]args)
  {
   userPrompt(); //Can't figure out how I'm supposed to set this up.
  }

  public static String userPrompt()
  {
   System.out.println("Enter 5 to 10 numbers separated by spaces, then press enter: ");
   String num = in.nextLine();

   return num; //I think I'm supposed to call the averager method here somehow?
  }

  public static double averager(String userPrompt)
  {
  double nums = Double.parseDouble(userPrompt);
  double average = 0;
  int counter = 0;
  char c = ' ';
       for (int i = 0; i < userPrompt.length(); i++)                           
       {
                if(userPrompt.charAt(i) == c)                                       
            {
                counter++;                                                      
            }
            average = nums / counter;
        }

    return average;
  }

      public static void result(double average, String userPrompt)
  {
      System.out.println("The average of the numbers" + userPrompt + "is" + average);
  }

}

GUIDELINES:
The program prompts the user for five to ten numbers, all on one line, and separated by spaces. Then the user calculates the average of those numbers, and displays the numbers and their average to the user.
The program uses methods to:
Get the numbers entered by the user Calculate the average of the numbers entered by the user Print the results with the whole number, a decimal, and two decimal positions The first method should take no arguments and return a String of numbers separated by spaces.
The second method should take a String as its only argument and return a double (the average).
The third method should take a String and a double as arguments but have no return value.
For example, if the user input is... 20 40 60 80 100 ...the program should give as output... The average of the numbers 20 40 60 80 100 is 60.00.


Solution

  • I will not exactly provide complete solution to your questions but guide you in solving the problem:

    • User input : 1 2 3 4 5 Thus, now you need to read it in a String which you are already doing in your userPrompt() method.
    • Post that you need to call your averager() method to get the average of the numbers. In that averager method you can need to split the String to get the numbers. Check : String.split() method documentation on how to achieve that. Then, you need to call Double.parseDouble() for your String array of numbers.
    • Finally , you need to make a call to result method in you main() method.

    I hope it helps you on how to approach the problems and has sufficient hints to get the correct solution