试图从文件中读取int。我的问题是,我的代码读取第一行中的所有整数,但在接下来的行中跳过第一个数字。 为了说明我的文件具有这些数目: 1,4,6,7,8,11,如图9所示, 1,4,6,7,8,11,12, 2,4,6,7,11, 12,0,跳过第一个数字,从文件java中读取int
但是,当我使用下面的代码它只打印1 4 6 7 8 11 9然后4 6 7 8 11 12然后2 ...它跳过第一个号码。
Scanner file = null;
ArrayList<Integer> listtwo = new ArrayList<>();
try {
file = new Scanner(new File(filename+".txt")).useDelimiter(",| ");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while(file.hasNext()){
if (file.hasNextInt()){
listtwo.add(file.nextInt());
}
file.next();
}
for (Integer i: listtwo) System.out.println(i);
@ambigram_maker错误。 'hasNext ...()'方法不会跳过任何内容。 –