0
有人可以解释为什么这段代码不起作用吗? 我的数组没有得到填充任何-.-从文件到数组传递整数
Integer[] tab1 = new Integer[401];
int[][] tab2 = new int[20][20];
File fr;
int i = 0, c = 0;
fr = new File("problem11");
Scanner sc;
try {
sc = new Scanner(fr);
while (sc.hasNext()) {
// System.out.printf("%d ", sc.nextInt());
tab1[i] = sc.nextInt();
i++;
System.out.print(tab1[i]);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
也就是说很可能是因为在您运行程序的位置的缺省目录中不存在“problem11”。 – dasblinkenlight
也许sc没有下一行,可能在阅读时出现问题,... 您的代码是否进入while循环?是否有堆栈跟踪? – Stultuske
如果它有整数,那么你应该得到hasNextInt而不是hasNext。你可能会得到InputMismatchException? – SMA