What can I do about method undefined and void errors in java?

Viewed 27

This code is supposed to take a list a movie reviews and compile them into a list then print that list. But I keep getting this error that the "The method addRating(double) is undefined for the type Review".

Everything seems to be fine syntax wise, might be wrong though. I'd like to know why this is happening and what I can do about it.

public class Activity2D {
public static void main(String[] args) {
      BufferedReader input;
      String title, ratingText;
      double rating = 0.0;

      Review[] movies = new Review[100];
      int size = 0;
      Review match;
      //Read in a file that has a list of movies and their reviews line by line
      //First the movie title, then the rating
      //Then put them in a list. No title repeats
      try {
         input = new BufferedReader(new FileReader("movies.txt"));

         title = input.readLine();
         System.out.println(title);
         while (title != null) {
            ratingText = input.readLine();
            System.out.println(ratingText);

            try {

               rating = Double.parseDouble(ratingText);

               
               match = findReview(movies, size, title);
               if (match == null) {
                  // movie that was not previously listed
                  movies[size] = new Review(title, rating);
                  size++;
               } else {
                  // this movie was already reviewed at least once
**The Error Happens Here...**
                  match.addRating(rating);
               }
            } catch (NumberFormatException nfe) {
               System.out.println("Invalid rating: " + ratingText);
            }

            title = input.readLine();
            System.out.println(title);
         }

         input.close();
      } catch (IOException ioe) {
         System.out.println(ioe.getMessage());
      }

      for (int i = 0; i < size; i++) {
         System.out.println(movies[i]);
      }

      System.out.println("\nFinished processing.");
   }
   //Find if a movie title/rating is already in a list
   public static Review findReview(Review[] movies, int size, String title) {
       Review result = null;
      int pos;

      pos = 0;
      while (pos < size && result == null) {
         if (movies[pos].matchTitle(title)) {
            result = movies[pos];
         } else {
            pos++;
         }
      }

      return result;
   }
}

class Review{
   private String title;
   private double totalRating;
   private int reviewCount;
   
   public Review(String title, double rating) {
       this.title = title;
       totalRating = rating;
       reviewCount = 1;
   }
   //Add rating to the movie review
**The Method is here, I don't know what the problem is :(**
   public void addRating(double rating) {
       totalRating += rating;
       reviewCount++;
   }
   //Compares 2 movies titles and returns if they are the same or not
   public boolean matchTitle(String otherTitle) {
       // returns true if this movie's title matches otherTitle
       return title.equals(otherTitle);
   }
   //Print stuff about the movie review
   public String toString() {
       return title + " (rating: " + (totalRating / reviewCount) + ")";
   }
}
0 Answers
Related