运行下面的代码时出现编译错误。在try-catch块之后的if语句中,编译器无法“解析为变量”。不过,我已经初始化了这些变量。任何帮助将不胜感激。
public void Load(UniversityListing uListing, StudentBody sBody) {
try {
FileInputStream fIn1= new FileInputStream(filename1);
BufferedReader bufferedReader1 = new BufferedReader(new InputStreamReader(fIn1));
existence1 = true;
}catch(FileNotFoundException ex){
System.out.println("Unable to open file '" + filename1 +"'");
existence1 = false;
}catch(IOException ex){
System.out.println("Error reading file '" + filename1+ "'");
existence1 = false;
}
if (existence1 == true){
LoadUListing(uListing, bufferedReader1,fIn1);
}
}
答案 0 :(得分:0)
当你在try块中声明并初始化 fIn1 和 bufferedReader1 时,它们将无法在try之外访问。
尝试以下更改:
public void Load(UniversityListing uListing, StudentBody sBody) {
BufferedReader bufferedReader1 = null;
FileInputStream fIn1 = null;
try {
fIn1 = new FileInputStream(filename1);
bufferedReader1 = new BufferedReader(new InputStreamReader(fIn1));
existence1 = true;
} catch (FileNotFoundException ex) {
System.out.println("Unable to open file '" + filename1 + "'");
existence1 = false;
} catch (IOException ex) {
System.out.println("Error reading file '" + filename1 + "'");
existence1 = false;
}
if (existence1 == true) {
LoadUListing(uListing, bufferedReader1, fIn1);
}
}