为什么我的程序从文件读取时忽略零? 例如,以下是文件中的数字:
0001 0011 0010
然后这是我的输出:
1
11
10
这是我的代码:
File file = new File("num.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNext()) {
if (scanner.hasNextInt()) {
System.out.println(scanner.nextInt());
} else {
scanner.next();
}
}
答案 0 :(得分:6)
使用scanner.next()
代替scanner.nextInt()
。
使用scanner.nextInt()
将删除所有前导零,因为0001 == 1。