Question

I have a method outside of the main method that averages. At the end of the main I am calling on that averaging method. I am not understaning the propper way to 1. use variables I declare with other methods in this program. for example the sum which is defined in the calculate_average method. or The sum which is defined in the main method.

Please show me

  1. how to interchange variables in and out of the main method. interchangeably between methods that are not in main.

  2. call on a method outside of main in main to calculate the average.

Here is my current code.

import java.util.ArrayList;
import java.util.Scanner;

class programTwo
{

    private static Double calculate_average( ArrayList<Double> myArr )
    {
        Double Sum = 0.0;
        for (Double number: myArr)
        {
            Sum += number;
        }

    }

    public static void main( String[] args )
    {
        Scanner scan = new Scanner(System.in);
        ArrayList<Double> myArr = new ArrayList<Double>();
        double Sum = 0;
        int count = 0;
        System.out.println("Enter a number to be averaged, repeat up to 20 times:");
        String inputs = scan.nextLine();

    while (!inputs.matches("[qQ]") )
    {

        if (count == 20)
        {
            System.out.println("You entered more than 20 numbers, you suck!");
            break;
        }

        Scanner scan2 = new Scanner(inputs); // create a new scanner out of our single line of input
        myArr.add(scan2.nextDouble());
        count += 1;
        System.out.println("Please enter another number or press Q for your average");

        inputs = scan.nextLine();
    }
    Double average = calculate_average(myArr);
    System.out.println("Your average is: " + average);

    return Sum / myArr.size();

}}
Was it helpful?

Solution

You have this at the end of main:

return Sum / myArr.size();

You need to move it to calculate_average:

private static Double calculate_average( ArrayList<Double> myArr )
{
    Double Sum = 0.0;
    for (Double number: myArr)
    {
        Sum += number;
    }

    return Sum / myArr.size();
}

Given that main is void and an average makes no sense as an exit code even if it weren't, I'm assuming that is some kind of typographical error.

Some code review

  • Local variables should start with a lower case letter. So sum, not Sum
  • You can use the primitive double instead of a Double object. Using the object will cause boxing/unboxing overhead.
  • Indent your while loop and its contents by four more spaces so the loop beginning aligns vertically with the code above it because it's in the same block (the method's block).
  • Split those last 2 braces into separate lines
  • Line up the last two braces vertically with the matching opening brace
  • You've started by creating your calculate_average method, but continue breaking up main. It's too long. Too much is going on there.
  • Change if (count == 20) to if (count >= 20). Harder to mess up, then.

OTHER TIPS

i think you have static method, so you can call them bye using className.MethodName Directly.

Just Declare your method as public and you can call them outside also.

 programTwo.calculate_average(myArr)

No need to create object for calling static methods.

There's nothing I can see wrong with your code, except you are missing a return statement in your calculate_average method.

Also, if you name a method calculate_average, it should return the average not the sum.

Try this:

private static Double calculate_average( ArrayList<Double> myArr )
{
    Double sum = 0.0;
    for (Double number: myArr)
    {
        sum += number;
    }
    return sum/myArr.size(); // added return statement
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top