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
Re: Need help making program
Hmm, so how should i declare the array in the main?
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
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...
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?