Average and standard deviation methods returning as NaN

I'm making a program that gets the averages and standard deviation of the numbers that are imputed in an array. I'm stuck on only one thing though: whenever the average and standard deviation are returned, it shows up as Not a Number. What is wrong with the code?

Here's the methods:

    public mainFrame() {
    initComponents();
}

private double avg;
private double stdDeviation;
private double[] gradeArray;

    /**
     *
     * @param gradeArray
     * @param numElem
     * @return
     */
    public double getAverage (double[] gradeArray, int numElem) {
    double sum = 0;

    for(int i=0;i<numElem;i++){
        sum = sum + gradeArray[i];
    }
    return (sum/numElem);        
}

public double getStdDev (double[] gradeArray, int numElem, double avg){
    double sum = 0;

    for(int i = 0;i<numElem;i++){
        sum = sum + Math.pow((gradeArray[i]-avg),2);
    }
    return Math.sqrt(sum/numElem);

And here is the event code when the "Enter Grades" button is clicked on:

final int SIZE = 25;
double[] gradeArray = new double[SIZE];
String s;
int numElem = 0;
double average = 0;

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

    s = (String)JOptionPane.showInputDialog("Enter Grade","");
    if (s == null || s == ("")){
        break;

    }
    else{
        try{
            gradeArray[i] = Double.parseDouble (s);
            average = getAverage(gradeArray,numElem);
            stdDeviation = getStdDev(gradeArray,numElem,avg);
            txtGradeEntered.setText(1 + i + "");
            txtMean.setText(average +"");
            txtStdDev.setText(stdDeviation+"");

        }
        catch (NumberFormatException e){
            JOptionPane.showMessageDialog(null, "Your input must be numeric!","Bad  Data!",0);
        }
        catch (ArrayIndexOutOfBoundsException e){
            JOptionPane.showMessageDialog (null, "You can only input 25 values!","Too much data!",0);
        }


    }
}

Answers


The problem is with division with such statement: return (sum/numElem); where numElm is initialized as0 and sum is also 0 because the following never gets executed:

for(int i=0;i<numElem;i++){ //<---- numElem = 0
        sum = sum + gradeArray[i];
    }

A NaN value is used to represent the result of certain invalid operations such as dividing zero by zero. NaN constants of both float and double type are predefined as Float.NaN and Double.NaN.

So initialize or set this numElem to some other value.


Need Your Help

Get extension from Content-Type

c# content-type file-extension

This is a very straight forward question.

XmlSlurper parsing a query result

parsing grails groovy xmlslurper

I have a query that bring back a cell in my table the has all xml in it. I have it so I can spit out what is in the cell without any delimiters. Now i need to actually take each individual element ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.