第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

在文件中查找特定號(hào)碼

在文件中查找特定號(hào)碼

BIG陽 2021-10-17 15:40:31
您好,我正在嘗試在 java 中循環(huán)一個(gè)文件,并僅輸出其中年份為 2000 的字符串。出于某種原因,當(dāng)我這樣做.trim().compare(year)時(shí)仍然返回所有字符串。我不知道為什么文件中的字符串示例是20/04/1999-30300913/04/2000-279906/10/1999-123例如,在這 3 個(gè)中,我只想獲取13/04/2000-2799 (注意文件很大)這是我到目前為止想出的代碼:public static void main(String[] args) throws IOException {    //Initiating variables    String filedir =("c://test.txt");    ArrayList<String> list = new ArrayList<String>();    String year = "2000";    try (Scanner scanner = new Scanner(new File(filedir))) {        while (scanner.hasNextLine()){            //  String[] parts = scanner.next().split("-");            if (scanner.nextLine().trim().contains(year)) {                System.out.println(scanner.nextLine());            }         }    } catch (IOException e) {        e.printStackTrace();    }}
查看完整描述

3 回答

?
江戶川亂折騰

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超5個(gè)贊

您代碼中的問題出在 while 塊中:


    while(scanner.hasNextLine()){              

          //This first call returns 13/04/2000-2799

          if(scanner.nextLine().trim().contains(year)){//This line finds matching value

                //But this line prints the next line                                      

                System.out.println(scanner.nextLine());//this call returns 06/10/1999-123

          }

    }

您可以做的是將您需要的值存儲(chǔ)在一個(gè)變量中,如果它與年份匹配,則打印它:


    while(scanner.hasNextLine()){

          //You store the value

          String value = scanner.nextLine().trim();

          //See if it matches the year

          if(value.contains(year)){

                //Print it in case it matches

                System.out.println(value);

          }

    }

希望這可以幫助。


查看完整回答
反對(duì) 回復(fù) 2021-10-17
?
慕田峪4524236

TA貢獻(xiàn)1875條經(jīng)驗(yàn) 獲得超5個(gè)贊

您使用了 scanr.nextLine() 兩次。那是一個(gè)錯(cuò)誤。每次迭代僅調(diào)用一次并將結(jié)果分配給 String 值以供使用。


查看完整回答
反對(duì) 回復(fù) 2021-10-17
?
慕斯王

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超2個(gè)贊

您調(diào)用了scanner.nextLine()兩次,這意味著一旦找到匹配的行,您實(shí)際上是在打印下一行。


查看完整回答
反對(duì) 回復(fù) 2021-10-17
  • 3 回答
  • 0 關(guān)注
  • 169 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)