hi i have made a program to find average and standard deviation. n it read input from other. rest of things are fine plz guide me my output for Standard deiation sD comes NaN. i have searched alot bt couldnot find solution
my code is

import java.io.*;
import java.util.*;

public class JephreyCuma {

/** Creates a new instance of JephreyCuma */
ArrayList list = new ArrayList();
public JephreyCuma() {
}

/**
* @param args the command line arguments
*/
public void fileReader(){
String line="";
try{
BufferedReader inp = new BufferedReader(new FileReader("c:/Users/dfff/Java/abc.txt"));
line = inp.readLine();
int number=0;
while(line!=null){
list.add(line);
line = inp.readLine();
}

}catch(Exception e){
System.out.println("Error converting "+line+" into number"); 
}
}

public double findArithmeticMean(){
double mean =0;
int sum=0;
for(int index=0;index<list.size();index++)
sum=sum+Integer.parseInt((String)list.get(index));

mean = sum/list.size();
return mean;
}


public double findSD(){
double sum=0;
double sumSQ=0;
double numSQ=0;
double avgSQ=0;
double var=0;
double SD=0;

for(int i=0;i<list.size();i++){
int num = Integer.parseInt((String)list.get(i));
double mean =0;

for(int index=0;index<list.size();index++)
sum=sum+Integer.parseInt((String)list.get(index));

mean = sum/list.size();
sum=sum+num;
numSQ=num*num;
sumSQ=sumSQ+numSQ;
avgSQ=sumSQ/list.size();
var=avgSQ-(mean*mean);
SD=Math.sqrt(var);


}
return SD;
}



public static void main(String[] args)throws IOException {
// TODO code application logic here
JephreyCuma jeph = new JephreyCuma();
jeph.fileReader();
System.out.println("Here is the standard deviation number: "+jeph.findSD());

System.out.println("Here is the Arithmetic mean: "+jeph.findArithmeticMean());
}

}

Look at the API again, saw that the way you create ArrayList is not fit your use.
http://download.oracle.com/javase/6/docs/api/

OK, the problem with declaring with no type will result in warning when you compile because you do not check for the raw type.

Another big problem is that you have NO indentation in your code. As a result, you could easily miss what you are doing.

Line 49 to 65, you have unnecessary loops inside. Also, it seems to be incorrect.

for(int i=0;i<list.size();i++){
  int num = Integer.parseInt((String)list.get(i));
  double mean =0;

  for(int index=0;index<list.size();index++)  // you compute sum for every number?
    sum=sum+Integer.parseInt((String)list.get(index));
 
  mean = sum/list.size();  // mean is always the same in the loop
  // not sure the computation below is correct???
  sum=sum+num;
  numSQ=num*num;
  sumSQ=sumSQ+numSQ;
  avgSQ=sumSQ/list.size();
  var=avgSQ-(mean*mean);
  SD=Math.sqrt(var);
}
return SD;

Is ever the incoming number a zero? That could cause problem.

*** Adding ***

Anyway, even though it is OK to compute mean in the loop, you should compute mean outside the loop and use the result value inside the loop.

While you are going through the loop, what you need to collect is the "different sum" of the square of (the number and its mean). Then after you finish the loop, you could easily compute the standard deviation.

SD = SQRT((Different Sum of each number compared to the mean)^2 / list size)

Edited 5 Years Ago by Taywin: n/a

no my problem lies in findSD() block. i have made array list to store numbers which it reads from file

*** edited *** Please take a look at my previous post. If you don't get it, below is somewhat an example of the code skeleton.

double sum = 0;
  for(...)  // compute sum
    ...
  double mean = sum/list.size();
  // or you could just call your findMean method to get the "mean" value

  for(...){
    int num = Integer.parseInt((String)list.get(i));
    numSQ = (num-mean)*(num-mean);
    sumSQ += numSQ;
  }
  // compute SD after you get sumSQ and N
  ...
return SD;

PS: The "..." is what you need to fill in with your code.

Edited 5 Years Ago by Taywin: n/a

This question has already been answered. Start a new discussion instead.