扫描仪无法解析

时间:2013-09-27 21:28:46

标签: java

    File inputTXT = new File (fileName);

    try{
    Scanner in = new Scanner(inputTXT);
    }catch(FileNotFoundException e){
        System.out.println("");
    }
        while(in.hasNext()){
            String line = in.nextLine();

它说无法解决。我该如何解决这个问题?

我尝试忽略了try catch块,但是这个文件扫描程序必须在try catch块中

1 个答案:

答案 0 :(得分:0)

在你的代码中你有关于变量scope的问题。你在try catch块中定义了in变量然后你在while循环中使用它。它应该如下所示:

    File inputTXT = new File (fileName);
    Scanner in=null;
    try{
     in = new Scanner(inputTXT);
    }catch(FileNotFoundException e){
        System.out.println("");
    }
        while(in.hasNext()){
            String line = in.nextLine();
相关问题