程序到达list[counter] = fi.nextInt();
时跳出该方法,为什么?
Scores.txt 目前包含“0 0 0 0 0 0 0 0 0 0”,由于某种原因,每次程序运行时它都会被删除为空白。控制台没有异常,我用的是eclipse。
代码:
int[] getValues() throws Exception{
Scanner fi = new Scanner(new File("./src/oppgave_2_v2/Scores.txt"));
int[] list = new int[10];
for(int counter = 0; counter < list.length; counter++){
list[counter] = fi.nextInt();
}
fi.close();
return list;
}
最佳答案
nextXXX() 类型的方法假定字符串中的下一个字符是 XXX 类型。如果你尝试 nextInt() 并且第一个字符是字母 p -_-
,它不会给你任何东西。关于Java从文件扫描,结束方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21195208/