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 12-11-2007, 11:08 AM
Member
 
Join Date: Dec 2007
Posts: 3
gemtez is on a distinguished road
working out wrong average
hello, i want to work out an average of different books' ratings , but my code is working out the wrong average, also i want to group the ratings that are for the same book (if they are entered more than once).

--------------------------------------------------------

import java.util.Scanner;

public class bookReview4
{

public void readBookTitles(String [] bookTitles)
{

}

public void readRatings(String [] bookTitles)
{ int count=0;
double sum=0;
int arrayposition;
int book=0;
// double average;
int rating=0;

//String [] bookTitles;

int numberofbooks=count+1;

double [] ratingSum= new double [numberofbooks];
Scanner keyboard = new Scanner(System.in);

while(rating<bookTitles.length)
{

System.out.println("please put rating for "+bookTitles[rating]);
ratingSum[count]= keyboard.nextInt();
//sum+=ratingSum[count];
// =rating;
rating++;
//count++;
}
for (rating= 0; rating<bookTitles.length; rating++)
{

sum += ratingSum[count];
}
double average=sum/3;
//average =
System.out.println("average is "+ average);

// for(int i=0 ;bookTitle[rating]=bookTitle[i]; i++)
// {
//
// System.out.println("average for"+bookTitle[i]+ "is ");
// }

//Give user ratings
if (average >=0 && average<1)
{
System.out.println("No rating ");
System.out.println();
}

else if (average >=1 && average<2)
{
System.out.println("Waste Paper ");
System.out.println();
}

else if (average >=2 && average<3)
{
System.out.println("Dissappointing ");
System.out.println();
}

else if (average >=3 && average<4)
{
System.out.println("A good read ");
System.out.println();
}

else if (average >=4 && average<5)
{
System.out.println("Excellent book ");
System.out.println();
}

else if (average >=5)
{
System.out.println("A must read! ");
System.out.println();
}

}
}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-11-2007, 12:09 PM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
On a quick glance:
Code:
//count++;
Why do you have the commented out? Dont you want to iterate over the array of books?

Also when you have a problem, it helps to say exactly what your problem is. Saying its "wrong" does no one any good.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-11-2007, 04:03 PM
Member
 
Join Date: Dec 2007
Posts: 3
gemtez is on a distinguished road
Quote:
Originally Posted by staykovmarin View Post
On a quick glance:
Code:
//count++;
Why do you have the commented out? Dont you want to iterate over the array of books?

Also when you have a problem, it helps to say exactly what your problem is. Saying its "wrong" does no one any good.
i try and do this and the program doesnt run properely and stops a the second entry,i want to do it so that it works out the average for all the books combined AND if for e.g. book2 has been entered twice work out an average for each book (book2 +book2/2) that has been entered, and print out what the highest rated book is, which in this case is book2, its not working out the average as a floating point or some reason, do you see what i mean and what i am trying to do?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-11-2007, 11:58 PM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
No, i have no idea what "it doesnt run properly" means. Does it give you an error? Is the answer wrong? If it is wrong, what is the answer, and what SHOULD the answer be? Is that you whole code? Also it helps when you use the [ code] [/ code] tags.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 12-12-2007, 10:35 AM
Member
 
Join Date: Dec 2007
Posts: 3
gemtez is on a distinguished road
yes it is my whole code, it does run there isn't any syntax errors, but its only working out an average for the last entry i input, i want it to work out an average for all the books, so for e.g. if i input book1, book1, book2, book3, i want it to recognise that book1 has been entered twice BUT it is the same book, and work out an average and give it the ratings.... i know its something to do with in.nextInt(); i'm not sure exactly what this does ive been told its appropriate but i dont know what to do with it or where in the code it will go., thanks for your help, much appreciated by the way
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
Calculate Average sthack99 New To Java 4 06-13-2008 01:09 PM
sum,product,average program chitwood New To Java 7 01-24-2008 07:18 AM
I need help with my java servlet homework(average) jellyfish888 Java Servlet 2 12-23-2007 11:57 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:27 AM.


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