2014-10-07 75 views
0

我從一個BufferedReader文本,我需要一個特定的字符串Java如何在特定字符串之後找到特定行?

後得到一個特定的行例如,這是文字:

<img src="https://example.com/image1.jpg"> 
<img src="https://example.com/image2.jpg"> 
<img src="https://example.com/image3.jpg"> 
<img src="https://example.com/image4.jpg"> 
<all> 
<img src="https://example.com/image9.jpg"> 
<img src="https://example.com/image11.jpg"> 
<img src="https://example.com/image15.jpg"> 

例如與斷行

<img src="https://example.com/image1.jpg"> 
<img src="https://example.com/image2.jpg"> 
<img src="https://example.com/image3.jpg"> 
<img src="https://example.com/image4.jpg"> 
<all> 

<img src="https://example.com/image9.jpg"> 
<img src="https://example.com/image11.jpg"> 
<img src="https://example.com/image15.jpg"> 

例子與其他字符

<img src="https://example.com/image1.jpg"> 
<img src="https://example.com/image2.jpg"> 
<img src="https://example.com/image3.jpg"> 
<img src="https://example.com/image4.jpg"> 
<all> 
UTUYGBLK 
<img src="https://example.com/image9.jpg"> 
<img src="https://example.com/image11.jpg"> 
<img src="https://example.com/image15.jpg"> 

I o只需要在'all'之後的img src行,然後繼續循環並結束循環。圖像編號是隨機的,所以我無法擊中任何這些。

目前我的代碼如下。

while ((line = rd.readLine()) != null) { 

    if (line.contains("all")) {  
    line = rd.readLine(); 
    System.out.println(line); 
    } 
} 

但如果是「所有」我會得到一個空返回後斷行...或有趣的人物UTUYGBLK

+1

如果遇到'「all」'並設置一個標誌並繼續循環。然後返回'!line.trim()。isEmpty()'的第一行。 (順便說一句,如果下一行是空白的,你的當前代碼不應該返回null,而是一個空字符串,只有當文件中的最後一行匹配時,纔會返回'null',在這種情況下,沒有「行後」 ) – 5gon12eder 2014-10-07 12:57:31

回答

2

您需要循環再元素之後,直到結果不爲空。下面的代碼應該可以防止在讀取文件結束後讀取行,並且在讀取完第一行之後停止讀取。

boolean loop = true; 
// global loop 
while ((line = rd.readLine()) != null && loop) { 
    // line with "all" in it is found? 
    if(line.contains("all")) {  
    // Continue going through the input until end of input 
    while((line = rd.readLine()) != null && loop) { 
     // When the line is not empty you have found the line after all! 
     if(!line.isEmpty()) { 
     System.out.println(line); 
     // to stop the loops 
     loop = false; 
     } 
    } 
    } 
} 
+0

表達式行!= null將始終爲真,因爲(line = ...)!= null是循環條件的一部分。 – UniversE 2014-10-07 13:14:28

+0

你是對的,編輯。 – Juru 2014-10-07 13:24:16

1

然後只是跳過空白行。

while ((line = rd.readLine()) != null) { 

    if (line.contains("all")) { 
     while ((line = rd.readLine()) != null && line.trim().isEmpty()); 
     System.out.println(line); 
     break; 
    } 
} 
+0

不包括輸入案例的結尾。 – Juru 2014-10-07 13:03:33

+1

@Juru現在確實如此。我認爲必須至少有一條定義有效的線 - 但我們永遠不會知道;) – UniversE 2014-10-07 13:08:04

+0

我從來不會假設太多:)。這種想法多次回到我的報復。 – Juru 2014-10-07 13:11:07

0
String line = ""; 
while (!line.contains("all")) 
    line = rd.readLine(); 
line = rd.readLine(); 
while ("".equals(line.trim())) 
    line = rd.readLine(); 
return line; 
//or System.out.println(line); 

這將:

  1. 設置line爲空字符串。
  2. 讀取行,直到獲取包含all的行。
  3. 閱讀下一行。
  4. 直到它讀取一個非空行。
  5. 返回它讀取的最後一個東西。

這是假定真的會出現一個包含all的行,並且確實會有一個非空白的地方。

+0

爲什麼downvote? – 2014-10-07 14:41:42

-1

您可以使用該方法

line.isEmpty() 

來處理這種情況

+0

所以我可以變得更好 - 爲什麼投下了票? – Puttzy 2014-10-07 13:45:38

0

你可以進入第二個循環來檢查空值。

while ((line = rd.readLine()) != null) { 

     if (line.contains("all")) {  
      while ((line = rd.readLine()) == null || line.isEmpty()) { 
        //keep looping while line is null 

        //check if it is no longer ready to avoid infinite loop 
        if (!rd.ready()) {break;} 
      } 
      //line should not be null here 
      System.out.println(line); 
     } 

或者對此有影響的東西。

+1

當您處於輸入結束時,無限循環。 – Juru 2014-10-07 13:04:30

+0

@Juru感謝您的高舉。現在怎麼樣?我添加了一個ready()檢查。 – 2014-10-07 13:08:09

+0

更好,但你也可能有一個空字符串而不是null,null表示輸入結束,空字符串意味着一個空行。 – Juru 2014-10-07 13:09:34

0

如果您使用contains()(如果您的圖像名稱包含「all」),則您的結果將不正確。修剪線條後,最好使用equals()。所以代碼將如下所示:

while ((line = rd.readLine()) != null) { 
    if (line.trim().equals("<all>")) { 
     while ((line = rd.readLine()).isEmpty()); 
     System.out.println(line); 
     break; 
    } 
} 
相關問題