我正在开发一个名为MovieDatabase的项目。对于我的方法searchTitle,searchGenre,searchDirector和searchYear,这些方法应该采用子字符串,看看它们是否可以在数据库中的任何位置找到。我现在编译但是当我测试BlueJ项目中的方法时,它们只返回null。我有什么改变让他们回归正确的东西?这是我的完整代码(P.S.那里有一个名为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 :(得分: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()
,请检查isEmpty()
生成的arraylist,以确保结果不为空。