java - Java - 为什么从文件读取时跳过零
问题描述
为什么我的程序在读取文件时会忽略零?例如,以下是文件中的数字:
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();
}
}
解决方案
使用scanner.next()
而不是scanner.nextInt()
.
使用scanner.nextInt()
将删除任何前导零,因为 0001 == 1。
推荐阅读
- php - Laravel 中带有 ID 和 Slug 的 URL 的路径函数
- python - 在python中改变海龟的颜色
- email - 域名转移后邮件崩溃
- python - 如何将 torchvision.datasets.ImageFolder 加载到 google colab ram
- ruby-on-rails - 如何在 Rails ActiveRecord 中返回生成的列?
- python - Python + 破折号 + 引导
- python - 抓取编码错误的 html
- vue.js - 在 HTML 中循环对象时,我需要帮助在 Vuejs2 中调用 watcher
- r - 将 csv 导入 R,提取多边形并使用 googlePolylines 包对其进行解码
- excel - VBA 自动化不适用于新的 IE 选项卡