# Need help making program

• December 6th, 2009, 02:20 AM
ixjaybeexi
Need help making program
Hey guys I have to create a program that computes the mean, standard deviation, and finds the max and min of a group of numbers read in from a file. I'm having trouble storing the objects from the file in an array and then reading it to compute the mean. Here's what I have so far:

Code :

```import java.io.*; import java.util.*;   public class Assignment5 {     public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("nerd.txt")); double[] data = new double[1000]; double mean = 0; double sum = 0; double n = 0;   while (input.hasNextDouble()){ double element = input.nextDouble(); data[element]++; n++;   }     for(int x = 0; x < data.length; x++){ sum = sum + data[x] * x; } mean = (double)sum / n; System.out.println("The mean is " + mean); }       }```

I don't really understand arrays. But basically I have to: Use an array to store the numbers being read. Keep a count each time a number is read (this will be N).

I just really don't know how to store the numbers from the file :/. Any help will be greatly appreciated :D
• December 6th, 2009, 03:08 AM
Json
Re: Need help making program
Hello there.

I believe you need to change your while loop slightly.

Code :

```while (input.hasNextDouble()) { double element = input.nextDouble(); data[n] = element; n++; }```

Doing it this way means that each element you reach from the file gets stored in the array in position n which is incremented after each store. I know this is just a basic program to get this going however you might want to think about what happens when there are 1001 or more numbers in file, your array will not be able to cope.

Happy programming and good luck :)

// Json
• December 6th, 2009, 03:32 AM
ixjaybeexi
Re: Need help making program
Hmm, so how should i declare the array in the main?
• December 6th, 2009, 04:07 AM
Json
Re: Need help making program
Its declared fine as it is right now.

Code :

`double[] data = new double[1000];`

Or did you mean if you want to avoid running into an ArrayIndexOutOfBoundsException because of the size?

Well you could use an ArrayList for instance as it will grow automagically as you populate it with more data.

// Json
• December 6th, 2009, 04:59 PM
ixjaybeexi
Re: Need help making program
Alrighty, hopefully now my logic will be correct and I'll be finished. Woo! Just one thing...when I go to compile, it says i declared "n" as a double, but an int is required. I didn't declare anythign as an int in my progrm so I don't undersand what is going on here...
• December 6th, 2009, 10:36 PM
ixjaybeexi
Re: Need help making program
Ok, I got a bit of it done, it's just that when there's a negative number in the list, it prints out the mean is NaN. An suggestions on how to fix my code to constitute for negative numbers?