# calcAverage() not calculating properly

• September 24th, 2013, 09:47 AM
FantomOptik
calcAverage() not calculating properly
I have a program that takes in 5 test scores, averages them and then assigns a letter grade to the average. I can't seem to get it to calculate properly. Can anyone point me in the right direction?

Here is the method used for averaging:
Code java:

``` public double calcAverage() { //calculate the average testscore double average = 0;   for (int i=0; i<nums.length;i++) { average+=nums[i]; average/=nums.length; }   //return the average score return average; }```

Here is where I have the button event:
Code java:

```for(int i=0; i<txtNums.length; i++) { scan = new Scanner(txtNums[i].getText()); if(scan.hasNextDouble()) { nums[i] = scan.nextDouble(); output = calcAverage(); } else { nums[i] = 0; txtNums[i].setText(" Error "); error = true; } }```
• September 24th, 2013, 10:09 AM
KevinWorkman
Re: calcAverage() not calculating properly
How do you calculate the average without a computer?

I recommend stepping through your program with a debugger, or at least adding some print statements, to figure out exactly where the program's execution differs from what you expect. What is the value of average each iteration of the loop?
• September 24th, 2013, 10:34 AM
FantomOptik
Re: calcAverage() not calculating properly
I just figured out what my issue was. I am trying to figure out how to mark it as solved.
• September 24th, 2013, 10:47 AM
KevinWorkman
Re: calcAverage() not calculating properly
You should be able to go to edit post (might have to open it in a new tab, that's a known forum bug) and mark it as solved. You might want to make a follow-up reply explaining what it was, that way anybody with similar problems will learn from what you did.