英文:
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<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();
}
}
答案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<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();
}
}
答案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<>(); // 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论