Results 1 to 2 of 2
Thread: beginners error
- 10-31-2011, 07:20 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 1
- Rep Power
- 0
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)
- 10-31-2011, 08:07 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Java beginners
By ivan.spasov90 in forum New To JavaReplies: 4Last Post: 10-26-2011, 04:55 PM -
Error from a beginners tutorial
By freelance84 in forum New To JavaReplies: 3Last Post: 07-22-2011, 04:39 PM -
Beginners fault
By NGE in forum New To JavaReplies: 6Last Post: 06-23-2011, 07:28 PM -
Help with beginners program
By DanK in forum New To JavaReplies: 6Last Post: 12-15-2008, 05:02 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks