2011-10-06 52 views
0

我剛剛從oracle.com網站複製,並做了一些細微的改變(改變控制檯到System.out),我編譯。無法追查爲什麼這個正則表達式代碼運行無限

但它運行無限。

import java.util.regex.Pattern; 
import java.util.regex.Matcher; 

class RegexTest { 

    public static void main(String[] args){ 

     while (true) { 

      Pattern pattern = 
      Pattern.compile("int"); 

      Matcher matcher = 
      pattern.matcher("int void int mathint"); 

      boolean found = false; 
      while (matcher.find()) { 
       System.out.printf("I found the text \"%s\" starting at " + 
        "index %d and ending at index %d.%n", 
        matcher.group(), matcher.start(), matcher.end()); 
       found = true; 
      } 
      if(!found){ 
       System.out.printf("No match found.%n"); 
      } 
     } 
    } 
} 

回答

3

while(true)將繼續運行,直到您break;出來。

+0

或者直到拋出一個異常。 – Thomas

+0

是的,他們也應該被抓住並相應處理。 – Kaivosukeltaja

2

好了,你在你的代碼while (true) { - 這可能「是一個無限循環:d

+0

+1「* might *」;-) – Tomalak

相關問題