【问题标题】:Why doesn't my error check work?为什么我的错误检查不起作用?
【发布时间】:2015-02-04 02:10:13
【问题描述】:

我所做的是每次用户输入无效的文件名时都会循环执行错误检查:

    public static Scanner readFile(String filename){
    Scanner stdin = new Scanner(System.in);
    File input = new File(filename);
    Scanner sc = null;
    do {
        try {
            sc = new Scanner(input);
    }
        catch(FileNotFoundException e) {
            System.out.println("Filename not valid. Please try again:");
            filename = stdin.nextLine();
    }
} while (!new File(filename).exists()); 
            return sc;
    }

我也有足够的方法来读取该文件并将数据放入数组中:

public static CO2Data[] readData(String filename){
File input = new File(filename);
    Scanner sc = null;
    try{
        sc = new Scanner(input);
    }
    catch(FileNotFoundException e){
        System.out.println("Filename not valid");
        System.exit(-1);
    }
String info = sc.nextLine();
int total = sc.nextInt();
CO2Data[] arr = new CO2Data[total];
for(int i=0; i<10;i++){
    arr[i] = new CO2Data();
    }
for(int i=0; i<10;i++){ 
    arr[i].setCountry(sc.next());
    arr[i].setTotalCO2(sc.nextDouble());
    arr[i].setRoadCO2(sc.nextDouble());
    arr[i].setCO2PerPerson(sc.nextDouble());
    arr[i].setCarsPerPerson(sc.nextInt());
    }
return arr;
}

问题是,如果我先输入一个无效的文件名,然后再输入一个有效的名称,程序会说该文件无效,但是如果我先输入一个有效的文件名,那么程序就可以正常工作。那么为什么首先输入一个有效的文件名可以正常工作,但输入一个无效的名称,然后输入一个有效的名称会使程序给我一条错误消息。

【问题讨论】:

    标签: java methods error-handling filenames


    【解决方案1】:

    让我们来看看完整的场景:

    如果你输入了一个无效的文件名,代码会移动到 catch 块,等待你用该行输入一个新的文件名

    filename = stdin.nextLine();

    你输入一个有效的文件名,变量文件名被重置为新的文件名(到目前为止很好)

    然后我们转到While 块,它将检查文件是否存在(它确实存在!)好,所以我们转到返回语句(包含问题)

    在 return 语句中,您返回持有 previous invalid 文件名的扫描仪。在退回之前,您需要使用新文件重新制作扫描仪。

    我可能会编辑 while 循环来读取

    public static Scanner readFile(String filename){
    Scanner stdin = new Scanner(System.in);
    File input = new File(filename);
    Scanner sc = null;
    do {
        try {
            input = new File(filename);
            sc = new Scanner(input);
        }catch(FileNotFoundException e) {
            System.out.println("Filename not valid. Please try again:");
            filename = stdin.nextLine();
        }
    } while (!input.exists());
        return sc;
    }
    

    这样我们在收到有效输入后创建一个新的File 类,然后返回一个新的Scanner 保存新的input

    【讨论】:

    • return 语句有问题。为了返回新的扫描器,必须捕获或抛出 filenotfound 异常。
    • @DavidRolfe 错误现在很好哈哈,抱歉星期五我的大脑变慢了
    • 即使是编辑过的程序仍然给我同样的问题。我想知道的是,该方法或调用这些方法的方式有问题吗?
    • @DavidRolfe 嗯。你能用错误信息更新你的答案吗?如果有的话,可能是堆栈跟踪
    【解决方案2】:

    您不会从文件名创建新输入 - 因此您始终使用扫描仪中的第一个文件名。这应该有效:

    更新代码

    public static Scanner readFile(String filename){
        Scanner stdin = new Scanner(System.in);
        Scanner sc = null;
        File input;
        do {
            input = new File(filename);
            try {
                sc = new Scanner(input);
            }
            catch(FileNotFoundException e) {
                System.out.println("Filename not valid. Please try again:");
                filename = stdin.nextLine();
            }
        } while (!input.exists());
        return sc;
    }
    

    【讨论】:

    • 使用该代码时,如果我第一次输入有效的文件名,它会发生与以前完全相同的事情,但如果我在输入一个无效的文件名后跟一个有效的文件名,则它不起作用。跨度>
    • 你仍然需要更改while行来检查input是否存在,而不是新文件,否则会出现同样的问题。 ^^
    • 问题 2. 我将 while 循环更改为 while (!input.exists());但是我收到一个编译器错误,提示找不到变量输入。我确定我之前在代码中声明了它,但编译器找不到它。
    • @DavidRolfe 哦,那是因为他将File input = new... 行移到了循环内部。如果你把那条线留在外面,只使用input = new File(filename);,它会起作用。 (这就是我对更新答案所做的)
    • 您不必重用输入变量,这个版本应该可以工作。我试过了,它要求一个文件名,直到它得到现有文件,然后返回扫描仪。你在哪里使用这个功能?
    猜你喜欢
    • 1970-01-01
    • 2016-09-17
    • 2012-04-02
    • 2021-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多