Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-23-2008, 03:24 AM
Member
 
Join Date: Nov 2007
Posts: 20
SCS17 is on a distinguished road
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 );

}

}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-23-2008, 04:06 AM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 263
roots is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-23-2008, 05:38 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,574
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to 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) ); } }
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-23-2008, 11:45 AM
Member
 
Join Date: Nov 2007
Posts: 20
SCS17 is on a distinguished road
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 11:51 AM.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-23-2008, 12:19 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,574
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to 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?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with displaying search results from an array BHCluster New To Java 4 04-24-2008 04:34 AM
problem displaying GUI from jbase diveshsingh AWT / Swing 0 01-30-2008 09:07 PM
Iterating through result set in JSTL Java Tip Java Tips 0 01-15-2008 04:13 PM
Iterating through result set in JSTL Java Tip Java Tips 0 01-14-2008 10:31 AM
getting a random result gradon New To Java 2 07-19-2007 04:54 PM


All times are GMT +3. The time now is 08:26 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org