2012-03-29 67 views
-3

我遇到了我的项目问题。这是一个电影虚拟数据库。我有两个类,MovieEntry(小,单个条目)和MovieDatabase(大,下面的代码)。我得到一个错误,返回我的ArrayLists,如searchYear。它说不兼容的类型。为什么我不能返回ArrayList?无法返回ArrayList

import java.util.ArrayList; 
import java.util.Scanner; 
import java.io.*; 

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<MovieEntry> searchTitle(String substring){ 
     for (MovieEntry title : Database) 
      System.out.println(title); 
      return null; 
    } 

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

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

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

    public ArrayList<MovieEntry> searchYear(int from, int to){ 
     ArrayList <String> Matches = new ArrayList<String>(); 
     for(MovieEntry year : Database); 
      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(){ 
    ArrayList <String> gList = new ArrayList<String>(); 
    for(MovieEntry m : Database){ 
     String g = m.getGenre(g); 
     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(d); 
     if(dList.contains(d) == false){ 
      dList.add(d); 
     } 
     return dList.size(); 
    } 

    } 

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




} 
+0

什么是确切的错误? – 2012-03-29 15:26:31

+1

请正确缩进您的代码 - 目前这非常具有误导性。 (就我个人而言,我也建议*总是*使用开放/关闭花括号,但这是一个不同的问题。) – 2012-03-29 15:27:06

回答

3

您在返回ArrayList <String>ArrayList<MovieEntry>预期。
ps:即使你的代码编译,看起来好像有很多错误。

2
public ArrayList<MovieEntry> searchYear (int yr){ 
    ArrayList <String> yearMatches = new ArrayList<String>(); 
    for (MovieEntry m : Database) 
     m.getYear(yr); 
    if(yearMatches.contains(yr) == false){ 
     String sYr = Integer.toString(yr); 
     yearMatches.add(sYr); 
    } 
    return yearMatches; 
} 

应该是MovieEntry对象,而不是String对象的列表返回值。

0
ArrayList <String> yearMatches 

应该

ArrayList<MovieEntry> yearMatches,因为你的方法签名是

ArrayList<MovieEntry> searchYear (int yr) 
4

你声明你的函数返回ArrayList<MovieEntry>和你正在返回的变量声明为ArrayList<String>。你需要改变一个或另一个。

2
public ArrayList<MovieEntry> searchYear (int yr){ 
    ArrayList <String> yearMatches = new ArrayList<String>(); 
    // ... 
    return yearMatches; 
} 

你的方法签名,表示您正在返回一个ArrayList<MovieEntry>但你实际上是返回一个ArrayList<String>

0

您需要更改yearMatches的返回类型searchYear

0

的方法指定你要返回一个ArrayList的,但你试图返回一个ArrayList中。

将ArrayList更改为ArrayList,它应该工作。否则你必须构造MovieEntry对象放入你的列表。

希望它有帮助。

0

ArrayList<MovieEntry>ArrayList <String>不兼容。只是不要输入回报(但我不认为是正确的)

0

我要猜测错误在您的searchYear方法中。您的退货类型为ArrayList<MovieEntry>,但是,您返回的是ArrayList<String>,因此为错误。

1

让我们看一下这个代码的细节,就像一个例子:

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

首先,让我们来解决的压痕,并加括号为清楚:

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

因此,这提出了问题:

  • 你为什么打电话m.getYear(yr),完全忽略了结果?
  • 你为什么要问ArrayList<String>是否包含int?它怎么可能?
  • 为什么你期望能够返回ArrayList<String>作为ArrayList<MovieEntry>?您只将年份的字符串值添加到yearMatches - 这不是MovieEntry

最后一部分是编译时间错误的原因......但是其余的代码简单地被破坏了。我怀疑你想要:

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

你忘了'(MovieEntry year:Database);'。非常令人费解 – UmNyobe 2012-03-29 15:32:39

+0

@UmNyobe:这是一种不同的方法。我只是在看这个具体的方法。 – 2012-03-29 15:44:23