Java-为什么从文件读取时跳过零

时间:2018-10-09 18:35:21

标签: java file

为什么我的程序从文件读取时忽略零? 例如,以下是文件中的数字:

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();
        }
    }

1 个答案:

答案 0 :(得分:6)

使用scanner.next()代替scanner.nextInt()

使用scanner.nextInt()将删除所有前导零,因为0001 == 1。

相关问题