我正在开发一个为电影创建虚拟数据库的项目。我有两个类:MovieEntry(用于单个电影条目)和MovieDatabase(包含数据库并允许添加的较大类等) 我得到的错误是“找不到符号 - 方法添加”,但我在顶部声明了一个ArrayList。这是我的全部代码,我知道还有其他错误。
**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>();
}
}**
我也有一个错误,说方法getYear()无法找到,即使它在另一个类中,它不是静态的,我正在创建一个调用另一个类的对象(MovieEntry m)。
编辑:我的searchTitle方法仍然出现错误,指出数据库是不兼容的类型。有人可以解释我的for-each循环的问题吗?我尽我所能地跟着这本书。到目前为止,感谢您的所有帮助!
答案 0 :(得分:0)
您的构造函数定义了一个隐藏类变量的局部变量Database
。正确地:
public MovieDatabase(){
Database = new ArrayList<String>(0);
}
主要问题是,当您致电Database.add(m);
时,您正在向MovieEntry
添加ArrayList<String>
实例,该实例仅支持添加字符串。
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);
}
答案 1 :(得分:0)
在searchYear方法中,Matches.add()应该是Matches.add(s);
答案 2 :(得分:0)
for (String s : Database)
s.getYear();
是String对象,在String类中没有像getYear()这样的方法。所以找不到方法错误
答案 3 :(得分:0)
这个
public MovieDatabase(){
ArrayList<String> Database = new ArrayList<String>(0);
}
应该是
public MovieDatabase(){
this.Database = new ArrayList<String>(0);
}
getYear函数不起作用,因为你有字符串对象的arraylist和字符串对象没有函数getYear。您必须创建一个新类,它将扩展String并将其放入arraylist泛型。
答案 4 :(得分:0)
主要问题:
数据库变量应该是私有的,不要在构造函数中创建新变量。
private ArrayList<String> Database;
public MovieDatabase(){
this.Database = new ArrayList<String>();
}
您的搜索方法返回列表,但没有return语句。返回null,或者init空arraylist返回它。例如:
public ArrayList<MovieEntry> searchTitle(String substring){
ArrayList<MovieEntry> entries = new ArrayList<MovieEntry>();
for (String title : Database)
System.out.println(title);
return entries;
}
}