Results 1 to 2 of 2

Thread: beginners error

  1. #1
    sidra 100 is offline Member
    Join Date
    Oct 2011
    Posts
    1
    Rep Power
    0

    Default beginners error

    ihave made a program to find average and standard dviation . it takes input from other file. value of average comes out correct but Sd comes out to b NaN. can any1 help me

    Java Code:
    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());
    }
    
    }
    Last edited by JosAH; 10-31-2011 at 07:52 PM. Reason: added [code] ... [/code] tags (but it didn't help much)

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default Re: beginners error

    System.out.println( ... ) is the ideal poor man's debugger; print out every interesting value (e.g. the length of the list of numbers etc.) and see if everything is as you expected.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Java beginners
    By ivan.spasov90 in forum New To Java
    Replies: 4
    Last Post: 10-26-2011, 04:55 PM
  2. Error from a beginners tutorial
    By freelance84 in forum New To Java
    Replies: 3
    Last Post: 07-22-2011, 04:39 PM
  3. Beginners fault
    By NGE in forum New To Java
    Replies: 6
    Last Post: 06-23-2011, 07:28 PM
  4. Help with beginners program
    By DanK in forum New To Java
    Replies: 6
    Last Post: 12-15-2008, 05:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •