gpt4 book ai didi

java - 变量 g 可能尚未初始化

转载 作者:行者123 更新时间:2023-12-02 07:53:30 28 4
gpt4 key购买 nike

我对我正在从事的这个项目有很多疑问。这是电影的虚拟数据库。我有一个小型 MovieEntry 类(用于处理单个条目)和一个大型 MovieDatabase 类,用于跟踪所有 10k+ 条目。在我的第二个 searchYear 方法以及后续方法中,我收到错误“变量 g(或 d 或其他)可能尚未初始化。”我还收到一个弹出错误,显示 Warnings from last Compilation: unreachable catch Clause。抛出的类型 java.io.FileNotFoundException 已经被捕获。 我对这两个问题都感到困惑。代码如下:

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<String> 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 m : Database);
m.getYear();
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>();
}




}

最佳答案

catch(IOException error){
System.out.println("Oops! Something went wrong.");
}

它告诉您 FileNotFoundException将处理 IOException 正在捕获的内容,因此 IOException 变得无法访问,因为它永远不会捕获 IO exceltion,为什么不捕获 Exception相反

至于初始化

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

线路String d = m.getDirector(d);可能是问题所在,d除非 MovieEntry 中有东西,否则不会被初始化,据我所知,永远不会有任何东西,因为您将其初始化为空数组列表

ArrayList<MovieDatabase> Database = new ArrayList<MovieDatabase>(0);

也许您应该将电影数组传递给构造函数,然后将这些电影添加到 Database变量?

关于java - 变量 g 可能尚未初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9931990/

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