[英]input.next() for one line with multiple values
I'm writing an adres directly from my code to a file with filewriter and those values are stored on one line in the file example(street 264, Washington). 我正在使用代码编写器将我的代码直接从我的代码写入文件,并且这些值存储在文件示例(华盛顿特区264号)中的一行上。 Now I want to write that adres to a new Arraylist with a constructor that asks for three input values(street, number, city)
现在,我想使用构造函数将其写入一个新的Arraylist中,该构造函数要求三个输入值(街道,数字,城市)
I have accomplished it this way but when I input a street like "Park Avenue" it gives an error as Park avenue is two words... also wondering if there is a faster "better" way: 我是用这种方式完成的,但是当我输入“ Park Avenue”之类的街道时,由于Park avenue是两个字而给出了一个错误……还想知道是否有更快的“更好”方式:
@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 reads multiple words so try using nextLine(); nextLine读取多个单词,因此请尝试使用nextLine();。 instead of next();
而不是next();
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));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.