0

I would like to take my current program and separate the averging function in to a method out side of the main method. I would like to store the numbers grabbed with the scanner in to an array list and then use my averaging method to grab those number and average the numbers together. then Output the average in place of the current System.out.println your average is..

Please help me illustrate this. I am having trouble understanding how all this comes together.

import java.util.Scanner;

class programTwo {

public static void main (String[] args) {
    Scanner scan = new Scanner(System.in);
    double sum = 0;
    int count = 0;
    System.out.println ("Enter your numbers to be averaged:");
    String inputs = scan.nextLine();

    while (!inputs.contains("q")) {
        Scanner scan2 = new Scanner(inputs); // create a new scanner out of our single line of input

         {
            sum += scan2.nextDouble();
            count += 1;
            System.out.println("Please enter another number or press Q for your average");
        } 

        if(count == 21)
        {
            System.out.println("You entered too many numbers! Fail.");
            return;
        }
        inputs = scan.nextLine();
    }
    System.out.println("Your average is: " + (sum/count));
}
}
7
  • Please be more specific. What are you having trouble with, exactly? Commented Feb 6, 2014 at 16:51
  • how do i take away the averaging function inside the main method. then create a new method that does the averaging outside the main. then call on it in the main. Commented Feb 6, 2014 at 16:52
  • 1
    By doing it. What have you tried? Commented Feb 6, 2014 at 16:53
  • you want me to post a bunch of variations of my code? Commented Feb 6, 2014 at 16:55
  • @Nicho If this is part of a school assignment, surely you would have been taught about methods and how to call them. As m0skit0 said, what is your current solution to this method extraction problem, and why is it not presently working? Commented Feb 6, 2014 at 16:56

3 Answers 3

1
//added an import here
import java.util.ArrayList;
import java.util.Scanner;

class programTwo
{
    //main difference is the average calculation is done within a method instead of main
    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:");
        String inputs = scan.nextLine();

    while (!inputs.contains("q")) //input until user no longer wants to give input
    {

        if (count == 21)
        {
            break; //this command here jumps out of the input loop if there are 21
        }

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

        inputs = scan.nextLine();
    }
    Double average = calculate_average(myArr); //go to method calculate average, expect a double to be returned
    System.out.println("Your average is: " + average); 
}

private static Double calculate_average( ArrayList<Double> myArr ) //method definition
{
    Double Sum = 0.0; 
    for (Double number: myArr) //for loop that iterates through an array list
    {
        Sum += number; //add all the numbers together into a sum
    }
    return Sum / myArr.size(); //return the sum divided by the number of numbers in the array list
}
}

This should help. Best of luck :)

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you... Will you please add some comments explaining the stuff you changed?
No problem, I know what it's like. I added some comments for you, it might be a little late now.
No way its not to late. The more help the better thanks again! Your the best
1
Average avg = new Average();

... avg.add(scan2.nextDouble());


System.out.println("Your average is: " + Average.result());


public class Average {
    void add(double x) { ... }
    double result() { ... }
}

The thinking up the implementation I leave to you.

2 Comments

The result is the same, of course, but according to the OP, they need to extract that portion of their code into a separate method; I take that to mean within the same class.
Thank you I appreciate you helping me.
0

Here is some example code

private static void hoot(List<Double> kapow)
{
    ... do all the stuffs
}

public static void main(final String[] arguments)
{
    List<Double> blam = new ArrayList<Double>();
    blam.add(1.1);
    blam.add(1.2);
    blam.add(1.3);

    hoot(blam);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.