2012-11-16 14 views
-2

雖然試圖獲得matcher.group()作爲字符串,它告訴它的不可用。爲什麼?Matcher.group()聲明的局部變量不可用

enter image description here

CODE:

private static ArrayList<String> validateList(List<String> listToProcess) { 

     List<String> resultTemp = new ArrayList<String>(); 
     boolean listIsCorrectlyFormatted = true; 


     String pattern = ""; 
     pattern = "(\\s)*(\\w\\w(\\w)*)((\\s)(\\w|-|\\.)+)?(((\\s)+|(\\s)+(,)*(\\s)*|(,)(\\s)*)+(\\+(\\d)+)+)?(((\\s)+|(\\s)+(,)*(\\s)*|(,)(\\s)*)+(\\w)+(\\w|\\s|-|,)*[^\\)|^\\(])?(((\\s)+|(\\s)+(,)*(\\s)*|(,)(\\s)*)+\\((\\w)+(\\w|\\s|-|,)*\\))?(\\s)*"; 
     // (\s)*(\w\w(\w)*)((\s)(\w|-|\.)+)?(((\s)+|(\s)+(,)*(\s)*|(,)(\s)*)+(\+(\d)+)+)?(((\s)+|(\s)+(,)*(\s)*|(,)(\s)*)+(\w)+(\w|\s|-|,)*[^\)|^\(])?(((\s)+|(\s)+(,)*(\s)*|(,)(\s)*)+\((\w)+(\w|\s|-|,)*\))?(\s)* 


     String nameP = "(\\w\\w(\\w)*)((\\s)(\\w|-|\\.)+)?"; 
     String plussP = "(\\+(\\d)+)+"; 
     String commentP = "((\\w)+(\\w|\\s|-|,)*[^\\)|^\\(])|(\\((\\w)+(\\w|\\s|-|,)*\\))"; 
     String tmpStr = ""; 
     int counter = 1; 
     Matcher matcher; 

     Pattern generalPt = Pattern.compile(pattern); 
     Pattern otherPattern = Pattern.compile(""); // * 

     for (String str : listToProcess) { 

      if (generalPt.matcher(str).find()) { 
       // OK 

       System.out.println("#"+counter+" :: OK ``"+str+"``"); 

       otherPattern = Pattern.compile(nameP); // name 
       matcher = otherPattern.matcher(str); 
       matcher.find(); 
       tmpStr += matcher.group(); // name 
       System.out.println("@[email protected]"+matcher.group()+"@"); 
       // ----------------------------------------------------- 
       matcher.reset(); 
       otherPattern = Pattern.compile(plussP); // plus 
       matcher = otherPattern.matcher(str); 
       matcher.find(); 

       tmpMatcherGroup = matcher.group(); 
       tmpStr += tmpMatcherGroup; 
       System.out.println("@[email protected]"+tmpMatcherGroup+"@"); 
       //-------------------------------------------------- 
       matcher.reset(); 
       otherPattern = Pattern.compile(commentP); // comment 
       matcher = otherPattern.matcher(str); 
       matcher.find(); 
       tmpStr += matcher.group(); 
       System.out.println("@[email protected]"+matcher.group()+"@"); 
       //-------------------------------------------------- 
       resultTemp.add(tmpStr); 

      } else { 
       // NOK 
       listIsCorrectlyFormatted = false; 

       tmpStr = "ERROR at line: # " + counter; 

       resultTemp.add(tmpStr); 

       System.out.println("#"+counter+" :: NOT OK ``"+str+"``"); 
      } 

      counter++; 
     } 

     List<String> result = new ArrayList<String>(); 
     result.add(Boolean.toString(listIsCorrectlyFormatted)); 
     result.addAll(resultTemp); 

     return (ArrayList<String>) result; 

    } 

堆棧跟蹤:

Thread [main] (Suspended (exception IllegalStateException)) 
    Matcher.group(int) line: not available 
    Matcher.group() line: not available [local variables unavailable] 
    DataProcess.validateList(List<String>) line: 111  
    DataProcess.main(String[]) line: 15 

SOLUTION:

matcher.reset(); 
       otherPattern = Pattern.compile(plussP); // plus 
       matcher = otherPattern.matcher(str); 
       matcher.find(); 
       try { 
        if (matcher.groupCount() > 0) { 
         tmpMatcherGroup = matcher.group(); 
        } 
       } catch (IllegalStateException e) { 
        System.out.println(e); 
       } 
       tmpStr += tmpMatcherGroup; 
       System.out.println("@[email protected]" + tmpMatcherGroup + "@"); 
+2

什麼局部變量? – Ankur

+0

「tmpMatcherGroup」爲空,因爲matcher.group()不返回(發生異常)。 –

+0

簡稱:JDK類不會與調試信息一起編譯,因此在調試過程中無法看到局部變量。 [描述](http://stackoverflow.com/questions/10266560/debugging-not-able-to-inspect-the-variables),[溶液](http://stackoverflow.com/questions/271521/locally-declared -variables-can-be-inspected) – CAMOBAP

回答

1

你需要先檢查groupCount()和如果它大於0,那麼你需要分配你的變量。

while(m.find()) { 
     for(int i = 0; i<=m.groupCount() ; i++) { 
      ...Your code 
     } 
    } 
+0

不工作,具有相同的異常 –

+0

你嘗試爲每matcher.group這個代碼() – Ankur

+0

@RCola編輯答案 – Ankur

1

的解釋是在the javadoc

拋出:

IllegalStateException - 如果沒有匹配尚未嘗試,或者以前的匹配操作失敗

相關問題