Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将变量传递到catch-Java中_Java_Android_Variables_Try Catch - Fatal编程技术网

将变量传递到catch-Java中

将变量传递到catch-Java中,java,android,variables,try-catch,Java,Android,Variables,Try Catch,我正在Android Studio中制作一个应用程序,它可以让你记录你坐过山车的次数,计算出你经历了多少重力等等 我希望rideCount变量在退出时保存,以便将其写入文件。然后,当该活动开始时,它将读取该文件并将其放入rideCount变量中。因为它在退出时写入,所以一开始文件中没有任何内容 当发生这种情况时,我希望它做的是将rideCount设置为0,并调用设置其他所有内容的方法,但我似乎无法将rideCount变量传递给catch位。有人能帮忙吗 先谢谢你 File file = new

我正在Android Studio中制作一个应用程序,它可以让你记录你坐过山车的次数,计算出你经历了多少重力等等

我希望
rideCount
变量在退出时保存,以便将其写入文件。然后,当该活动开始时,它将读取该文件并将其放入
rideCount
变量中。因为它在退出时写入,所以一开始文件中没有任何内容

当发生这种情况时,我希望它做的是将
rideCount
设置为
0
,并调用设置其他所有内容的方法,但我似乎无法将
rideCount
变量传递给catch位。有人能帮忙吗

先谢谢你

File file = new File("AltonAirCount.txt");

    try{
        Scanner input = new Scanner(file);
        int rideCountFile = input.nextInt();
        final int[] rideCount = {rideCountFile};
        onCreate2(rideCount);
    } catch (FileNotFoundException ex){
       //I want it to set rideCount to 0 here
      //I want it to call up onCreate2 and pass rideCount to it
    }}


ridecontfile
必须在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);
}
当然,除非您需要在以后未包含的代码中使用
ridecontfile
,否则在catch块中根本不需要它,因此代码可以简化为:

try{
    Scanner input = new Scanner(file);
    int rideCountFile = input.nextInt();
    final int[] rideCount = {rideCountFile};
    onCreate2(rideCount);
} catch (FileNotFoundException ex){
    onCreate2(new int[] {0});
}

谢谢你的回复。它修复了以前的问题,但是当我退出并重新加载应用程序时,变量重置为0。我已经添加了将文件写入我的初始帖子的代码部分,以防这部分是问题所在。
try{
    Scanner input = new Scanner(file);
    int rideCountFile = input.nextInt();
    final int[] rideCount = {rideCountFile};
    onCreate2(rideCount);
} catch (FileNotFoundException ex){
    onCreate2(new int[] {0});
}