swaguire - 7 months ago 26

Java Question

My code calculates the population deviation when I need it to calculate the sample deviation I have compared both formulas and tried changed my calculations but nothing seems to work. Thanks for everyone's help or input in advance.

`public class MeanAndStandardDeviation {`

public static void main (String argv []) throws IOException {

BufferedReader stdin =

new BufferedReader (new InputStreamReader (System.in));

NumberFormat nf = new DecimalFormat ("0.00");

nf.setMinimumFractionDigits (2);//Sets Min digits

nf.setMaximumFractionDigits (2);//Sets Max digits

String inputValue;

int count = 0;

//For Loop for count

for(int i = 0; i < count; i++){

count++;

}

double varianceFinal = 0;

List<String> input = new ArrayList<String>();//String ArrayList

List<Double> numbers = new ArrayList<Double>();//Double ArrayList

//While loop that takes in all my input and assigns it to the ArrayLists

//Parameters set for when null is entered and total numbers go over 500

while((inputValue = stdin.readLine()) != null && !inputValue.equals("") && input.size()<500){//Parameters set for when null is entered and total numbers go over 500

input.add(inputValue);

numbers.add (Double.parseDouble(inputValue));

}

System.out.println ("Standard Deviation: " +(nf.format(calcStdDev (numbers, count, varianceFinal))));//Prints the Standard Deviation

}

//StandardDeviation Class

static double calcStdDev (List<Double> numbers, int count, double variance){

variance = 0;

double sum = 0;

for(int i = 0; i < numbers.size(); i++){

sum += numbers.get(i);

variance += numbers.get(i) * numbers.get(i);

count++;

}

double varianceFinal = ((variance/count)-(sum*sum)/(count*count));

return Math.sqrt(varianceFinal);

}

}

Answer

Seriously, your code is "wrong" on many levels. So instead of debugging all of that for you, I will give you some hints how to **fix** and simplify your code - then it should be very easy for you to fix/resolve your actual math problem.

First of all, your code is so written in a confusing style that just makes it **much** harder to understand (and therefore *debug*) than it needs to be.

Example:

```
int count = 0;
//For Loop for count
for(int i = 0; i < count; i++){
count++;
}
```

That for loop doesn't do *anything*. And even when the condition would be something else, like `i < someNumber`

; you would still just need to put `count = someNumber`

there; instead of looping!

Same here: what is the point of providing **count** as argument to your calc methods?! And to then just increase it? So, lets rewrite that:

```
public static double calcStdDev (List<Double> numbers, double variance) {
double sumOfNumbers = 0;
double sumOfSquares = 0;
for(double number : numbers) {
sumOfNumbers += number;
sumOfSquares += number * number;
}
... and instead of calculating count, you simply have
int numberOfNumbers = numbers.size();
... and now, do your math
```

The other thing that is really strange in your code is how you setup your *variance* variable; and how it is used within your calc methods.

Long story short: step back and remove everything from your code that isn't required.