java - input.next() 用于具有多个值的一行
问题描述
我正在使用 filewriter 将地址直接从我的代码写入文件,这些值存储在文件示例(华盛顿 264 号街)的一行中。现在我想用一个构造函数将地址写入一个新的 Arraylist,该构造函数要求三个输入值(街道、数字、城市)
我已经通过这种方式完成了,但是当我输入像“公园大道”这样的街道时,它会给出错误,因为公园大道是两个词......还想知道是否有更快的“更好”方式:
@Override
public List<Adres> query(ISpecification specification) {
File adresConnection = new File(fsConnection.getAdresConnection());
if (specification instanceof FileSpecification) {
if (((FileSpecification) specification).toFileQuery().equals("ALL")) {
ArrayList<Adres> adressen = new ArrayList<>();
try (
Scanner input = new Scanner(adresConnection)) {
System.out.println("Adressen laden wordt uitgevoerd");
while (input.hasNext()) {
String straat = input.next();
String huisNrMetKomma = input.next();
int huisNummer = Integer.parseInt(huisNrMetKomma.substring(0, huisNrMetKomma.length() - 1));
String plaats = input.next();
adressen.add(new Adres(straat, huisNummer, plaats));
}
解决方案
nextLine 读取多个单词,因此请尝试使用 nextLine(); 而不是下一个();
while (input.hasNext()) {
String straat = input.nextLine();
String huisNrMetKomma = input.nextLine();
int huisNummer = Integer.parseInt(huisNrMetKomma.substring(0, huisNrMetKomma.length() - 1));
String plaats = input.nextLine();
adressen.add(new Adres(straat, huisNummer, plaats));
}
推荐阅读
- php - 在 laravel 查询生成器上的条件位置不返回任何结果
- php - 当我的页面重新加载而我没有点击“提交”时,我的邮件就被发送了
- spring - 如何在hibernate中映射数据库结构
- python - 错误 - JSONDecodeError:预期值:第 1 行第 1 列(字符 0)
- ios - iOS 如何将凭据添加到 ASCredentialIdentityStore
- django - 我应该使用 uWSGI 吗?
- python - docker化时pip要求不匹配
- bash - bash - 将变量从脚本加载到主 shell
- spring-boot - 如何实现多后端服务和解耦架构的单点登录
- python - django 如何在 HTML 中仅使用 {% url ' x' %} 发出 POST 请求