Skip to content

Commit

Permalink
Merge pull request sakaiproject#253 from ottenhoff/SAM-2508
Browse files Browse the repository at this point in the history
SAM-2508 a bit of protection from exceptions especially the NumberFormatException
  • Loading branch information
ottenhoff committed Mar 5, 2015
2 parents 71f4cc3 + 67ce54a commit 2ade189
Showing 1 changed file with 23 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -581,24 +581,33 @@ public boolean histogramScores(HistogramScoresBean histogramScores, TotalScoresB
if (obj != null && !"".equals(obj)) {
String[] objs = obj.split(",");
for (int i=0; i < objs.length; i++) {

// SAM-2508 set a default value to avoid the NumberFormatException issues
Double pctCorrect = 0.0d;
Double newAvg = 0.0d;
int divisor = 1;

try {
if (questionScores.getPercentCorrect() != null && !"N/A".equalsIgnoreCase(questionScores.getPercentCorrect())) {
pctCorrect = Double.parseDouble(questionScores.getPercentCorrect());
}
}
catch (NumberFormatException nfe) {
log.error("NFE when looking at metadata and objectives", nfe);
}

if (objectivesCorrect.get(objs[i]) != null) {
int divisor = objectivesCounter.get(objs[i]) + 1;
// newavg = avg + ((q3 - avg)/index)
Double newAvg = objectivesCorrect.get(objs[i]) + (
(Double.parseDouble(questionScores.getPercentCorrect()) - objectivesCorrect.get(objs[i])
) / divisor);

Double objCorrect = objectivesCorrect.get(objs[i]);
divisor = objCorrect.intValue() + 1;

newAvg = objCorrect + ((pctCorrect - objCorrect) / divisor);
newAvg = new BigDecimal(newAvg).setScale(2, RoundingMode.HALF_UP).doubleValue();

objectivesCounter.put(objs[i], divisor);
objectivesCorrect.put(objs[i], newAvg);
} else {
Double newAvg = Double.parseDouble(questionScores.getPercentCorrect());
newAvg = new BigDecimal(newAvg).setScale(2, RoundingMode.HALF_UP).doubleValue();

objectivesCounter.put(objs[i], 1);
objectivesCorrect.put(objs[i], newAvg);
newAvg = new BigDecimal(pctCorrect).setScale(2, RoundingMode.HALF_UP).doubleValue();
}

objectivesCounter.put(objs[i], divisor);
objectivesCorrect.put(objs[i], newAvg);
}
}

Expand Down

0 comments on commit 2ade189

Please sign in to comment.