Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
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 03-30-2008, 06:48 AM
Member
 
Join Date: Mar 2008
Posts: 3
sthack99 is on a distinguished road
Calculate Average
Can someone please tell me why my averages are coming up as 0s?

Code:
import java.io.*; import java.util.*; public class AverageScores { public static void main (String[] args) throws FileNotFoundException { double test1, test2, test3, test4, test5; String name; double average = 0.0; char grade; Scanner inFile = new Scanner(new FileReader("student_scores.txt")); PrintWriter outFile = new PrintWriter("student_scores.out"); outFile.println("Student Test1 Test2 Test3 Test4 Test5 Average Grade"); while (inFile.hasNext()) { name = inFile.next(); test1 = inFile.nextDouble(); test2 = inFile.nextDouble(); test3 = inFile.nextDouble(); test4 = inFile.nextDouble(); test5 = inFile.nextDouble(); calculateAverage(average, test1, test2, test3, test4, test5); outFile.printf("%-9s %5.0f %5.0f %5.0f %5.0f %5.0f %5.0f %n", name, test1, test2, test3, test4, test5, average); } inFile.close(); outFile.close(); } public static void calculateAverage(double a, double t1, double t2, double t3, double t4, double t5) { a = (t1 + t2 + t3 + t4 + t5) / 5.0; } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-30-2008, 07:52 AM
Member
 
Join Date: Mar 2008
Posts: 3
sthack99 is on a distinguished road
And just so everyone knows, calculateAverage needs to be a viod method, not a return method.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-12-2008, 06:40 PM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
Your calculateAverage method must be a double so that it could return a double value that will be assigned to average variable to be able to show the expected results,

eg.

average = calculateAverage(parameters);


This is intended for the guests who browse this thread for additional clarification.
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Last edited by sukatoa : 06-12-2008 at 06:43 PM.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-12-2008, 09:46 PM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 687
Nicholas Jordan is on a distinguished road
Why are we using printf() ~ that looks like something that was in the original spec as a design the instructor likes. Class Double will have a toString() method, as will the Sting class being able to take a double, likely at least and I would think so.

As you have it, calculate average may be declared as sukatoa shows and as well should have a counter of some kind, this avoids the nasty (1,2,3,4,5,6,7,8,9,0) parameter list style and lends to the loop style which your problem domain is natural.

We then are presented with the matter of passing the list, since this is obviously a learning exercise I suggest you consider how the list could be passed to the calc()

Skip worrying about how to do that with a file, there are coding constructs that will do this.

Last edited by Nicholas Jordan : 06-12-2008 at 09:47 PM. Reason: fix spelling of domain
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-13-2008, 01:09 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,402
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
That's much better.

The most better way I think is while read each double value, update the total of all. Then you comes with a single value(a double) to find the average.
__________________
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
working out wrong average gemtez New To Java 4 12-12-2007 10:35 AM
Calculate Tax in java toby New To Java 2 07-30-2007 11:03 AM
Calculate what e1 and e2 should be Legoland New To Java 11 07-02-2007 08:01 PM
get the average and maximum score Eric Advanced Java 2 07-01-2007 06:15 AM
Calculate average age for women and men? Legoland New To Java 3 04-18-2007 12:38 PM


All times are GMT +3. The time now is 04:52 AM.


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