Results 1 to 5 of 5
  1. #1
    SCS17 is offline Member
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0

    Default 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:

    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 );

    }

    }

  2. #2
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    Put this line just outside your loop ..
    Reason : your sum is one less due to last -1 input.
    average = (sum + 1)/counter ;
    dont worry newbie, we got you covered.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Not only that root. If you take that line of code outside of the while loop the correct one should be,

    Java 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.

    Java 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) );
        }
    }

  4. #4
    SCS17 is offline Member
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0

    Default

    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.
    Last edited by SCS17; 04-23-2008 at 10:51 AM.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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?

Similar Threads

  1. Problem with displaying search results from an array
    By BHCluster in forum New To Java
    Replies: 4
    Last Post: 04-24-2008, 03:34 AM
  2. problem displaying GUI from jbase
    By diveshsingh in forum AWT / Swing
    Replies: 0
    Last Post: 01-30-2008, 08:07 PM
  3. Iterating through result set in JSTL
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-15-2008, 03:13 PM
  4. Iterating through result set in JSTL
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-14-2008, 09:31 AM
  5. getting a random result
    By gradon in forum New To Java
    Replies: 2
    Last Post: 07-19-2007, 03:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •