[英]Scanner read only the first line of a textual file
这是我的代码
public static void main(String[] args) {
File source = //
Scanner s = null;
int lineNumber =0;
ArrayList<ArrayList<Integer>> tagsArray = new ArrayList<>();
try {
s= new Scanner(source);
while (s.hasNext()) {
String[] cols = s.nextLine().split(" ");
for (int i = 0; i < cols.length; i++) {
if (cols[i].equals("1"))
tagsArray.get(i).add(lineNumber);
}
lineNumber++;
}
} catch (Exception e) {
// TODO: handle exception
}
}
当我删除for语句时,它将读取整个文本文件,但是当我使用它时,它仅读取第一行,为什么?
我猜您会收到一个Exception
,但是您捕获并隐藏了它,而不是处理它。 这真是太糟了! 您至少应该打印异常的堆栈跟踪。
您尝试访问:
tagsArray.get(i).add(lineNumber);
当tagsArray
为空时。 在访问它之前,需要实例化tagsArray
每个ArrayList<Integer>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.