Results 1 to 5 of 5
  1. #1
    gemtez is offline Member
    Join Date
    Dec 2007
    Posts
    3
    Rep Power
    0

    Default 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();
    }

    }
    }

  2. #2
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    On a quick glance:
    Java 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.

  3. #3
    gemtez is offline Member
    Join Date
    Dec 2007
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by staykovmarin View Post
    On a quick glance:
    Java 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?

  4. #4
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    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.

  5. #5
    gemtez is offline Member
    Join Date
    Dec 2007
    Posts
    3
    Rep Power
    0

    Default

    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

Similar Threads

  1. Calculate Average
    By sthack99 in forum New To Java
    Replies: 4
    Last Post: 06-13-2008, 12:09 PM
  2. sum,product,average program
    By chitwood in forum New To Java
    Replies: 7
    Last Post: 01-24-2008, 06:18 AM
  3. I need help with my java servlet homework(average)
    By jellyfish888 in forum Java Servlet
    Replies: 2
    Last Post: 12-23-2007, 10:57 PM
  4. get the average and maximum score
    By Eric in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 05:15 AM
  5. Calculate average age for women and men?
    By Legoland in forum New To Java
    Replies: 3
    Last Post: 04-18-2007, 11:38 AM

Posting Permissions

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