wuno
wuno

Reputation: 9865

Creating an Array List

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));
}
}

Upvotes: 0

Views: 160

Answers (3)

TheOneWhoPrograms
TheOneWhoPrograms

Reputation: 659

//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 :)

Upvotes: 1

DwB
DwB

Reputation: 38290

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);
}

Upvotes: 0

Joop Eggen
Joop Eggen

Reputation: 109547

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.

Upvotes: 1

Related Questions