rideCountFile
必须在try块之前声明才能被catch块访问.
int rideCountFile; try{ Scanner input = new Scanner(file); rideCountFile = input.nextInt(); final int[] rideCount = {rideCountFile}; onCreate2(rideCount); } catch (FileNotFoundException ex){ rideCountFile = 0; // call onCreate2 again if you wish final int[] rideCount = {rideCountFile}; onCreate2(rideCount); }
当然,除非你需要rideCountFile
在后面的代码中使用你没有包含的内容,否则你在catch块中根本不需要它,所以代码可以简化为:
try{ Scanner input = new Scanner(file); int rideCountFile = input.nextInt(); final int[] rideCount = {rideCountFile}; onCreate2(rideCount); } catch (FileNotFoundException ex){ onCreate2(new int[] {0}); }