0

I'm working on a project that creates a virtual database for films. I have two classes: MovieEntry (for the individual movie entry) and MovieDatabase (the larger class that contains the database and allows for additions, etc.) The error I'm getting is "Can't Find Symbol - Method Add" but I have an ArrayList declared at the top. This is the entirety of my code, and I know there are other mistakes.

**public class MovieDatabase
{
   public ArrayList<String> Database = new ArrayList<String>();

   public MovieDatabase(){
       ArrayList<String> Database = new ArrayList<String>(0);
    }

   public int countTitles() throws IOException{
       Scanner fileScan;
       fileScan = new Scanner (new File("movies.txt"));
       int count = 0;
       String movieCount;
       while(fileScan.hasNext()){
           movieCount = fileScan.nextLine();
           count++;
        }
       return count;
    }

   public void addMovie(MovieEntry m){
       Database.add(m);
    }

   public ArrayList<MovieEntry> searchTitle(String substring){
       for (String title : Database)
          System.out.println(title);
    }

   public ArrayList<MovieEntry> searchGenre(String substring){
       for (String genre : Database)
          System.out.println(genre);
    }

   public ArrayList<MovieEntry> searchDirector (String str){
       for (String director : Database)
          System.out.println(director);
    }

   public ArrayList<MovieEntry> searchYear (int yr){
       ArrayList <String> yearMatches = new ArrayList<String>();
       for (String s : Database)
       s.getYear();
        if(yearMatches.contains(y) == false){
           yearMatches.add(y);
        }
       return yearMatches;
    }

   public ArrayList<MovieEntry> searchYear(int from, int to){
       ArrayList <String> Matches = new ArrayList<String>();
       for(Student s : movies);
          Matches.add();
       return Matches;
    }

   public void readMovieData(String movies){
       String info;
       try{
           Scanner fileReader = new Scanner(new File(movies));
           Scanner lineReader;

           while(fileReader.hasNext()){
               info = fileReader.nextLine();

               lineReader = new Scanner(info);
               lineReader.useDelimiter(":");

               String title = lineReader.next();
               String director = lineReader.next();
               String genre = lineReader.next();
               int year = lineReader.nextInt();
            }

        }catch(FileNotFoundException error){
            System.out.println("File not found.");

        }catch(IOException error){
            System.out.println("Oops! Something went wrong.");
        }
    }

   public int countGenres(){
    String g;
    ArrayList <String> gList = new ArrayList<String>();
    for(Student s : movies){
      String g = s.getGenre();
      if(gList.contains(g) == false){
        gList.add(g);
      }
      return gList.size();
    }
    }
    public int countDirectors(){
     ArrayList <String> dList = new ArrayList<String>();
     for(Student s : movies){
        String d = s.getDirector();
        if(dList.contains(d) == false){
            dList.add(d);
        }
        return dList.size();
     }

     }

    public String listGenres(){
        ArrayList <String> genreList = new ArrayList<String>();

    }

}**

I also have an error saying method getYear() cannot be found even though it's in another class, it's not static, and I'm creating an object (MovieEntry m) that invokes the other class.

EDIT: I am still getting an error in my searchTitle method that says Database is of an incompatible type. Can someone explain the problem with my for-each loop? I followed the book as best I could. Thanks for all your help so far though!

Michael Moore
  • 29
  • 3
  • 6

5 Answers5

0

Your constructor defines a local variable Database that hides the class variable. Correctly:

public MovieDatabase(){
    Database = new ArrayList<String>(0);
}

The main problem is that when you call Database.add(m); you're adding a MovieEntry instance to an ArrayList<String> that only supports adding Strings.

public class MovieDatabase
{
   public ArrayList<MovieDatabase> database = new ArrayList<MovieDatabase>();

   public MovieDatabase(){
   }

   public int countTitles() throws IOException{
       Scanner fileScan;
       fileScan = new Scanner (new File("movies.txt"));
       int count = 0;
       String movieCount;
       while(fileScan.hasNext()){
           movieCount = fileScan.nextLine();
           count++;
        }
       return count;
    }

   public void addMovie(MovieEntry m){
       database.add(m);
    }
jabal
  • 11,987
  • 12
  • 51
  • 99
  • Thank you jabal, that answer helped immensely. However the next error I get involves the searchTitle method. It highlights "Database" and says "incompatible types." Is that not where the ArrayList goes? I'm very new to for-each loops and the book says little. – Michael Moore Mar 29 '12 at 12:05
  • In `searchTitle` you do the very same mistake: you try to iterate over a list of `MovieEntries` with a String. Correctly: for(MovieEntry me: Database) { System.out.println(me.getTitle()); } – jabal Mar 30 '12 at 08:17
  • Java suggests naming variables with names starting with lowercase letters to distinct them from classes whose name starts with uppercase letter. – jabal Mar 30 '12 at 08:18
0

In the searchYear method, Matches.add() should probably be Matches.add(s);

Chetter Hummin
  • 6,687
  • 8
  • 32
  • 44
0
for (String s : Database)
   s.getYear();

s is a String object and in String class there is no method like getYear(). So method not found error

Chandra Sekhar
  • 18,914
  • 16
  • 84
  • 125
  • @MichaelMoore first of all change your first four lines of code to public ArrayList Database; public MovieDatabase(){ this.Database = new ArrayList(0); } and then reply me with the error. – Chandra Sekhar Mar 29 '12 at 12:08
0

This

 public MovieDatabase(){
    ArrayList<String> Database = new ArrayList<String>(0);
 }

should be

 public MovieDatabase(){
   this.Database = new ArrayList<String>(0);
 }

The getYear function doesn't work because you have arraylist with string objects and string objects dont have a function getYear. You have to create a new class that will extend the String and put that into the arraylist generic.

blejzz
  • 3,349
  • 4
  • 39
  • 60
0

Main problems:

Database variable should be private, and dont create new variable in constructor.

private ArrayList<String> Database;

public MovieDatabase(){
       this.Database = new ArrayList<String>();
    }

Your search methods returns list, but doesn't have return statement. Return null, or init empty arraylist an return it. For example:

   public ArrayList<MovieEntry> searchTitle(String substring){
       ArrayList<MovieEntry> entries = new ArrayList<MovieEntry>();
       for (String title : Database)
          System.out.println(title);
       return entries;
    }
}
bigGuy
  • 1,732
  • 1
  • 22
  • 37