2012-03-30 72 views
0

我正在研究一個名爲MovieDatabase的項目。對於我的searchTitle,searchGenre,searchDirector和searchYear方法,這些方法應該接受一個子字符串,並查看它們是否可以在數據庫中的任何位置找到。我現在編譯的內容是,但是當我測試BlueJ項目中的方法時,它們只返回null。我爲了讓他們返回正確的東西而改變什麼?這裏是我的代碼的全部(附註:有一個單獨的類名爲MovieEntry處理該get方法):困惑於返回值

public class MovieDatabase 
{ 
    private ArrayList<MovieEntry> Database = new ArrayList<MovieEntry>(); 
    public MovieDatabase(){ 
     ArrayList<MovieDatabase> Database = new ArrayList<MovieDatabase>(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<String> searchTitle(String substring){ 
     for (MovieEntry m : Database) 
      if(m.getTitle().contains(substring)){ 
      System.out.println(m.getTitle()); 
     } 
     return null; 
    } 

    public ArrayList<String> searchGenre(String substring){ 
     for (MovieEntry m : Database) 
     if(m.getGenre().contains(substring)){ 
      System.out.println(m.getGenre()); 
     } 
     return null; 
    } 

    public ArrayList<String> searchDirector (String substring){ 
     for (MovieEntry m : Database) 
     if(m.getDirector().contains(substring)){ 
      System.out.println(m.getDirector()); 
     } 
     return null; 

    } 

    public ArrayList<MovieEntry> searchYear(int year){ 
    ArrayList<MovieEntry> yearMatches = new ArrayList<MovieEntry>(); 
     for (MovieEntry m : Database) { 
     if (m.getYear() == year) { 
      yearMatches.add(m); 
     } 
    } 
    return yearMatches; 
    } 

    public ArrayList<MovieEntry> searchYear(int from, int to){ 
     ArrayList <MovieEntry> Matches = new ArrayList<MovieEntry>(); 
     for(MovieEntry m : Database){ 
      if(m.getYear() >= from && m.getYear() <= to){ 
      Matches.add(m); 
     } 
     } 
    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(IOException error){ 
      System.out.println("Oops! Something went wrong."); 
     } 
    } 

    public int countGenres(){ 
    ArrayList <String> gList = new ArrayList<String>(); 
    for(MovieEntry m : Database){ 
     String g = m.getGenre(); 
     if(gList.contains(g) == false){ 
     gList.add(g); 
     } 
    } 
      return gList.size(); 
    } 

    public int countDirectors(){ 
    ArrayList <String> dList = new ArrayList<String>(); 
    for(MovieEntry m : Database){ 
     String d = m.getDirector(); 
     if(dList.contains(d) == false){ 
      dList.add(d); 
     } 

    } 
      return dList.size(); 
    } 

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




} 
+0

我試過,但它說「找不到符號 - 變量m」或類似的東西。我認爲這是因爲m在for-each循環內,因此無法訪問。 – 2012-03-30 02:06:14

+0

您正在閱讀文件...但是您從未將任何信息放在我可以告訴的位置,檢查了您的數據列表中是否有任何內容? – 2012-03-30 02:08:24

回答

2

您必須修改

public ArrayList<String> search*(String substring){

以下性質的方法刪除System.out.println。相反,將匹配添加到新的字符串數組列表。

public ArrayList<String> searchTitle(String substring){ 
     ArrayList<String> matches = new ArrayList<String>(); 
     for (MovieEntry m : Database) 
      if(m.getTitle().contains(substring)){ 
      matches.add(m.getTitle()); 
     } 
     return matches; 
    } 

無論您呼叫的searchTitle(),檢查造成的ArrayList爲isEmpty(),以確保結果不爲空。