0

I am fairly new to java and I'm trying to code to find the average. I understand that the average is adding all the numbers and then dividing the sum by the number of numbers but I'm not really sure how to code that. My guess is that I'd need a for loop but I don't know what to do from there. The program basically asks for a file to be read and then calculate the average. Here's the code I have so far:

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class Calculations 
{

public static void main(String[] args) throws FileNotFoundException 
{
    System.out.println("Please enter a file name");
    Scanner keyboard = new Scanner(System.in);

    String filename = keyboard.next();
    Scanner reader = new Scanner (new File(filename));

    int length = reader.nextInt();

    double [] num = new double[length];
    double [] num2 = new double[length];

    System.out.println("The numbers are:");

    for(int i = 0; i < length; i++)
    {
        num[i] = reader.nextDouble();
        System.out.println(num[i]);
    }

}

}

The file I would be using is list.txt which contains:

20
    1.1 2 3.3 4 5.5 6 7 8.5 9 10.0

    11 12.3 13 14 15.5 16.1 17 18 19.2 20.0

The mean should be 10.625. Any help is deeply appreciated. Thank you in advance.

Jack
  • 11
  • 2
  • 4
  • 1
    Hi Jackie, what troubles are you actually facing? What results are you getting? – Scary Wombat Feb 13 '17 at 02:14
  • Is [this thread](http://stackoverflow.com/questions/7008189/calculate-average-in-java) a duplicate of your question? – Ted Hopp Feb 13 '17 at 02:18
  • First of all you need to give some context. Is this for some exercise and if so, are you limited to primitives and arrays? Do you have to account for huge files? Are there requirements on how to read in the File? Can you read everything to memory and then process the data as a whole? – Stefan Nolde Feb 13 '17 at 02:20

2 Answers2

0

Just introduce a new variable sum, initialize it to 0, and add the elements to the variable while you are printing them.

System.out.println("The numbers are:");
double sum = 0; //new variable
for(int i = 0; i < length; i++)
{
    num[i] = reader.nextDouble();
    sum += num[i];
    System.out.println(num[i]);
}
sum /= (double) length; //divide by n to get the average
System.out.print("Average : ");
System.out.println(sum);
Jarvis
  • 8,494
  • 3
  • 27
  • 58
  • Thank you so much! I'm pretty new at Java and I was really stuck on this, yet it was so simple. Thank you so much for taking the time to answer – Jack Feb 13 '17 at 02:25
0

It appears that you're simply having trouble computing the average; I'll address that issue here:

In Java 7 and below, use a for loop:

    double sum = 0; //declare a variable that will hold the sum

    //loop through the values and add them to the sum variable
    for (double d : num){
        sum += d;
    }
    double average = sum/length;

In Java 8 you can use a Stream to compute the average

    double sum = Arrays.stream(num).sum(); //computes the sum of the array values
    double average = sum/length; 
Austin
  • 8,018
  • 2
  • 31
  • 37