# problem with displaying result..

• 04-23-2008, 02:24 AM
SCS17
problem with displaying result..
hello there.. I have this simple application that it supposed to show the average of the numbers that you enter.. but I seem to have a mistake with the calculations.. i think it has to do with typecasting or something. The average is just not correct!! Any help would appreciated. Thanks.

the code:

Quote:

import java.util.*;

public class ex {

public static void main(String[] args) {

double sum = 0;
int counter = 0;
double average = 0;
double input = 0;

do {
System.out.print("ENTER A GRADE (OR ENTER -1 TO EXIT) ");
input = new Scanner(System.in).nextDouble();

sum+=input;

counter++;
average= sum / (float)counter;

}

while (input != -1);

System.out.println("\nThe average is " + average );

}

}
• 04-23-2008, 03:06 AM
roots
Put this line just outside your loop ..
Reason : your sum is one less due to last -1 input.
average = (sum + 1)/counter ;
• 04-23-2008, 04:38 AM
Eranga
Not only that root. If you take that line of code outside of the while loop the correct one should be,

Code:

`average = (sum + 1)/(counter - 1) ;`
because he update the counter also inside the while loop and for the input -1 also counter updates by one.

Here is much better way, which I really loved to used in my programming.

Code:

```import java.util.*; public class ex {     public static void main(String[] args) {         double sum = 0;         int counter = 0;         double input = 0;         do {             System.out.print("ENTER A GRADE (OR ENTER -1 TO EXIT) ");             input = new Scanner(System.in).nextDouble();                     if(input != -1) {                 sum += input;                 counter++;             }             else {                 break;             }         }while (true);         System.out.println("\nThe average is " + (sum/counter) );     } }```
• 04-23-2008, 10:45 AM
SCS17
I do see what you guys mean.. I actually know how to get the correct average using a for or a while loop but I just wanted to try it using a do-while.

I do see the mistake.. actually now I think of it, probably a do-while is not the best thing to use in these kind of things unless you wanna loop forever and have an if statement inside like Eranga did.

But isnt the do-while supposed to check the boolean condition in the while() before executing the body. ?? otherwise.. whats the use of using it.

So lets say I entered 1 the first time, then 1, then 1.. then the last time when I enter (-1).. the loop should check the boolean condition and determines what happens, so when I enter (-1), the loop should break.. you know I was thinkin that java will read the input and then jump to the while part to check the boolean condition.. thats what I was thinking!.

The flow of control here is kind of confusing.
I hope some of you guys can explain this a little bit if you dont mind.

Thanks alot.. appreciate it.
• 04-23-2008, 11:19 AM
Eranga
That is the purpose of do-while loop. In some situations(in real world application it is not rare actually) we need to do something and then test a condition. You can't do the same using while, isn't it?