【发布时间】:2017-12-22 00:27:03
【问题描述】:
我有一个 txt,我想扫描它,每次读取一个整数时,我都想将它输入到我已经创建的数组中。
当它读取除 int 以外的内容(例如 String、double 甚至是空行)时,如何抛出异常?
这就是我读取文件并完成数组的方式:
file= new Scanner(new File(file_name));
int[] txt = new int[cnt]; // cnt , the number of lines in my txt
while ( file.hasNextInt()) {
txt[count] = file.nextInt();
count++;
}
谢谢你:)
【问题讨论】:
-
阅读 javadocs - 如果您想处理其他内容,请不要使用
nextInt和hasNextInt