1

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(); }} 
8
  • make the calculate_average method static Commented Feb 8, 2014 at 5:58
  • @Neil isn't it already? Commented Feb 8, 2014 at 5:59
  • 1
    I'm unclear on your goal. Commented Feb 8, 2014 at 6:00
  • Hey look there it is - don't know why I didn't see it the first time... Commented Feb 8, 2014 at 6:01
  • I was just gonna edit my comment. I'm lost at what your actual question is. Commented Feb 8, 2014 at 6:01

3 Answers 3

2

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.
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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 } 

2 Comments

but its dividing the sum of the array but the size of the array to find the average. Is that a bad way to do that?
Yes. it's bad. Either call the method getSum() and return the sum, or call it getAverage() and return the average. Having a method name not agree with its behaviour leads to confusion and bugs. You could consider having a summing method that the average method calls.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.