uma nab
uma nab

Reputation: 45

how to solve NaN error

this is the code for calculating average and I'm getting NaN error when i run my project

public  static double calculateAverage(){
    double attendence = 0;
    int no_of_matches = 0;
    double average =0;
        for (Team t: ApplicationModel.getTeamList()){
        for(Match m : ApplicationModel.getMatchList()){
                if(m.getTeamname().equals(t.getName())){
                    attendence =+ (m.getAttendence());
                    no_of_matches ++;   
                }
            }
            average = attendence / no_of_matches ;
    } 
        return average;
}

this is the code of calling the calculating average method

String[] columnNames = {"Name","Coaches","League","Division","Fulltime","Number of Coaches","Average Attendence"};

 if (ApplicationModel.getTeamList()!=null){
     int arrayIndex=0;
     for (Team c :ApplicationModel.getTeamList()){
           String[] currentRow = new String[7];
           currentRow[0] = c.getNameAsString();
           currentRow[1] = c.getCoachesAsString();
           currentRow[2] = c.getLeague();
           currentRow[3] = c.getDivision();
           currentRow[4] = c.getFulltime();
           currentRow[5] = Integer.toString(c.getCoaches().length);
           currentRow[6] = Double.toString(c.calculateAverage());
           rowInfo[arrayIndex]=currentRow;
           arrayIndex++;
           teamDisplay.append(c.toString());
         }
        }

Upvotes: 1

Views: 4582

Answers (2)

Clashsoft
Clashsoft

Reputation: 11882

I think the problem might be this line of code:

attendence =+ (m.getAttendence());

Instead of adding the value to the total variable, you assign the total variable to the value. Another problem is the fact that you do not handle the case where no_of_matches (which is a terrible variable name in terms of naming conventions) is 0, i.e. there are no matches. Last of all, average = attendence / no_of_matches always re-assigns the average, thus discarding any results from the previous team.

Code Suggestion:

double attendence = 0;
int matches = 0;
for (Team t: ApplicationModel.getTeamList())
{
    for(Match m : ApplicationModel.getMatchList())
    {
        if(m.getTeamname().equals(t.getName()))
        {
            attendence += (m.getAttendence());
            matches++;
        }
    }
} 
return matches > 0 ? attendence / matches : 0D;

Upvotes: 3

CrApHeR
CrApHeR

Reputation: 2655

I think you can fix the NAN error checking if no_of_matches > 0 before use it in the division operation.

public static double calculateAverage(){
    double attendence = 0;
    int no_of_matches = 0;
    double average = 0;

    for (Team t: ApplicationModel.getTeamList()) {
        for (Match m: ApplicationModel.getMatchList()) {
            if (m.getTeamname().equals(t.getName())) {
                attendence =+ (m.getAttendence());
                no_of_matches ++;   
            }
        }

        if (no_of_matches > 0)
            average = attendence / no_of_matches ;
    }

    return average;
}

An aditional note, when you add this check and no_of_matches is 0 your average will be 0, that means you have no matches.

Hope this helps.

Upvotes: 0

Related Questions