I try to read a text file and saved the data in an Arraylist, but the Scanner read methods have an Scanner closed error

huangapple go评论101阅读模式
英文:

I try to read a text file and saved the data in an Arraylist, but the Scanner read methods have an Scanner closed error

问题

这是我的代码,City是另一个包含三个整数变量的类。我试着按照老师的指示来做,但出现了这个错误。我不知道为什么会发生这种情况。

public class Test {
    private static List<City> cities;

    public static void main(String[] args) {
        readFile("res/data.txt");// TODO code application logic here
    }

    public static void readFile(String filename) {
        try {
            Scanner sc = new Scanner(new File(filename));
            cities = new ArrayList<City>();
            while (sc.hasNext()) {
                cities.add(new City(sc.nextInt(), sc.nextInt(), sc.nextInt()));
            }
            sc.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}
英文:

Here is my code, The city is another class which contains three int
variable. I tried to follow our teachers' instruction, but there is
this error. I don't know why this happens.

public class Test {
  private static List&lt;City&gt; cities;
   
    public static void main(String[] args) {
        readFile(&quot;res/data.txt&quot;);// TODO code application logic here
    }
    public static void readFile(String filename){
     try {
            Scanner sc = new Scanner(new File(filename));
            cities = new ArrayList&lt;City&gt;();
            while (sc.hasNext()) {
                cities.add(new City(sc.nextInt(),sc.nextInt(),sc.nextInt()));
              sc.close();
            }          
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

答案1

得分: 1

你必须将这行代码移到循环外面:

sc.close();

这样修改后的代码如下所示:

public class Test {
    private static List<City> cities;

    public static void main(String[] args) {
        readFile("res/data.txt");// TODO code application logic here
    }
    
    public static void readFile(String filename) {
        try {
            Scanner sc = new Scanner(new File(filename));
            cities = new ArrayList<City>();
            while (sc.hasNext()) {
                cities.add(new City(sc.nextInt(), sc.nextInt(), sc.nextInt()));
            }
            sc.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}
英文:

You have to move this line

sc.close();

outside the while loop so

public class Test {
  private static List&lt;City&gt; cities;

    public static void main(String[] args) {
        readFile(&quot;res/data.txt&quot;);// TODO code application logic here
    }
    public static void readFile(String filename){
     try {
            Scanner sc = new Scanner(new File(filename));
            cities = new ArrayList&lt;City&gt;();
            while (sc.hasNext()) {
                cities.add(new City(sc.nextInt(),sc.nextInt(),sc.nextInt()));
            } 
            sc.close();         
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }

答案2

得分: 0

有明显的错误。你在循环中调用了 sc.close()。因此只会读取第一行。你需要将 sc.close() 移动到 while 循环之后。
希望能帮到你。

英文:

There is obvious error. You are calling sc.close() in a loop. So only the first line would be read. You have to move sc.close() to after the while loop.
Hope it helps

答案3

得分: 0

你的 sc.close() 调用是 while 循环中的最后一条语句。将它移到 } 后面。但是,如果出现异常,你当前的代码会泄漏文件句柄。这并不好。最好将 close 调用移到 finally 块中。更好的方法是,他们添加了 try-with-Resources,因为手动处理关闭操作很麻烦(而且正确处理关闭操作很困难)。使用它的话,代码会类似于:

public static void readFile(String filename){
    try (Scanner sc = new Scanner(new File(filename))) {
        cities = new ArrayList<>(); // 钻石操作符
        while (sc.hasNext()) {
            cities.add(new City(sc.nextInt(), sc.nextInt(), sc.nextInt()));
        }          
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
}
英文:

Your sc.close() call is the last statement in the while loop. Move it after the }. But, if there is an exception, your current code leaks a file handle. This is not good. Better to move the close call to a finally block. Even better, they added try-with-Resources because that is cumbersome (and correctly handling close is difficult). To use that, it would be something like

public static void readFile(String filename){
    try (Scanner sc = new Scanner(new File(filename))) {
        cities = new ArrayList&lt;&gt;(); // Diamond Operator
        while (sc.hasNext()) {
            cities.add(new City(sc.nextInt(),sc.nextInt(),sc.nextInt()));
        }          
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
}

答案4

得分: 0

sc.close(); 移出 while 循环。

英文:

Move sc.close();
out of while loop.

huangapple
  • 本文由 发表于 2020年5月4日 09:53:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/61584015.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定