簡體   English   中英

input.next()用於具有多個值的一行

[英]input.next() for one line with multiple values

我正在使用代碼編寫器將我的代碼直接從我的代碼寫入文件,並且這些值存儲在文件示例(華盛頓特區264號)中的一行上。 現在,我想使用構造函數將其寫入一個新的Arraylist中,該構造函數要求三個輸入值(街道,數字,城市)

我是用這種方式完成的,但是當我輸入“ 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讀取多個單詞,因此請嘗試使用nextLine();。 而不是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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM