public static ArrayList<String> readFile(File inputFile){
try{
Scanner in = new Scanner(inputFile);
ArrayList<String> codes = new ArrayList<String>();
while(in.hasNextLine()){
codes.add(in.nextLine());
}
in.close();
}catch(IOException exception){
exception.printStackTrace();
}catch(NoSuchElementException exception){
System.out.println(exception.getMessage());
}catch(Exception exception){
System.out.println(exception.getMessage());
}
return codes;
}
这段代码为何报错 codes cannot be resolved to a variable.(return那一行)
codes的生命期只限于try里面,提到前面就行了:
ArrayList<String> codes = null;
try里面直接给codes赋值