简体   繁体   English

input.next()用于具有多个值的一行

[英]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.

相关问题 在 Java 中,while(input.hasNext()) 内的多个 input.next() 触发 NoSuchElementException - Multiple input.next() inside of a while(input.hasNext()) triggers NoSuchElementException in Java 为什么有时会跳过input.next()? - Why is input.next() sometimes being skipped? 我可以在for循环的标题中使用input.next()吗? - Can I use input.next() in the header of a for loop? 为什么input.next()用于搜索ArrayList,而.nextLine()却不起作用? - Why does input.next() work for searching an ArrayList, but .nextLine() does not? 是否可以在验证后将input.next作为变量长度参数列表中的参数直接传递给方法? <in java> - Is it possible to pass input.next - after verification - directly to a method as parameters in a variable length argument list? <in java> 如何在Java的一行中输入多个字符串 - How to input multiple strings on one line in Java 从同一输入行读取多个值 - Reading multiple values from the same input line (Java)套接字的BufferedReader一次只占用一行,在接收下一行之前需要客户端输入 - (Java) BufferedReader of a socket only takes one line at a time, requires client input before receiving the next line 在输出的下一行获取输入 - Getting an input on the next line of an output 如何从 Java 的单行输入中读取多个 Integer 值? - How to read multiple Integer values from a single line of input in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM