gpt4 book ai didi

java - 找不到符号 - 方法添加

转载 作者:行者123 更新时间:2023-12-01 05:24:07 24 4
gpt4 key购买 nike

我正在开发一个为电影创建虚拟数据库的项目。我有两个类: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 循环的问题吗?我尽我所能地阅读了这本书。不过,感谢您迄今为止的所有帮助!

最佳答案

你的构造函数定义了一个局部变量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);
}

关于java - 找不到符号 - 方法添加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9924777/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com