2017-04-25 100 views
0

我的方法需要檢查給定的字符串是否等於定義的模板,實際上它不會按預期產生輸出。模板輸入字符串

Inputstring:18122016 with template「...」會正確地返回「二○一六年十二月一十八日」

Inputstring:15:00模板「:」將返回「15:500」,這是不對的,預期爲15:00

也許有人比我更清晰的解決方案?

這是我的測試用例:

@Test 
public void testDoCandidateWithTextTemplate() { 

    ArrayList<String> tWords = new ArrayList<>(); 
    tWords.add(":"); 
    String result = VORecognitionResultsImpl.doCandidateWithTextTemplate("1500", tWords, " :"); 
    assertEquals("15:00", result); 
    result = VORecognitionResultsImpl.doCandidateWithTextTemplate("15:00", tWords, " :"); 
    assertEquals("15:00", result); 

    result = VORecognitionResultsImpl.doCandidateWithTextTemplate("18122016", tWords, " . ."); 
    assertEquals("18.12.2016", result); 

    result = VORecognitionResultsImpl.doCandidateWithTextTemplate("1437", tWords, " ,"); 
    assertEquals("14,37", result);  

} 

這裏是方法

public static String doCandidateWithTextTemplate(String tmpTextCandidate, 
              ArrayList<String> tWords, 
              String textTemplate) 
{ 
    if(textTemplate == null) return tmpTextCandidate; 
    if(textTemplate.length() == 0) return tmpTextCandidate; 
    StringBuffer outText = new StringBuffer(); 
    int currentIndex = 0; 

    boolean isInserted = false; 

    for(int i = 0; i < textTemplate.length();i++){ 
     currentIndex = i; 
     if (textTemplate.charAt(i) == ' '){ 
      if (!isInserted){ 
       outText.append(tmpTextCandidate.charAt(i)); 
      } 
      else{ 
       outText.append(tmpTextCandidate.charAt(i-1)); 
       isInserted = false; 
      } 
     } 
     else{ 
      if (textTemplate.charAt(i) != tmpTextCandidate.charAt(i)){ 
       outText.append(textTemplate.charAt(i)); 
       isInserted = true; 

      } 
     } 
    } 

    if (currentIndex < tmpTextCandidate.length()-1){ 
     outText.append(tmpTextCandidate.substring(currentIndex-1, tmpTextCandidate.length())); 
    } 

    return outText.toString(); 

} 
+0

如果輸入爲「18123016」輸出爲18.13.3016,那麼在您的索引計算中存在錯誤時,您的代碼也可能會中斷。你只是不能做索引1,它必須是持久的,我的建議是使用兩個索引我和j,這將使您的代碼更具可讀性並且易於糾正 –

回答

3

你的計數器(currentIndex)是關閉的一個。典型的角落案例問題。

對於1500 - >15:500突破:在for條件斷裂後,i3currentIndex2因爲你沒有進入循環的第四次。所以當你做tmpTextCandidate.substring(currentIndex-1, tmpTextCandidate.length())tmpTextCandidate.substring(1, 4)這是500outString後分隔符(:)的部分。

您的第一個測試用例成功了,因爲您有兩個分隔符(。)的計數因此,有時發生兩個錯誤會產生正確的輸出,但錯誤仍然存​​在。

所以,如果您從tmpTextCandidate中獲取char,並且確保它不是一個關閉,那麼您應該只增加currentIndex,就像現在一樣。

編輯:我修正了函數中的錯誤,而不改變方法。

public static String doCandidateWithTextTemplate(String tmpTextCandidate, 
             ArrayList<String> tWords, 
             String textTemplate) 
{ 
    if(textTemplate == null) return tmpTextCandidate; 
    if(textTemplate.length() == 0) return tmpTextCandidate; 
    StringBuffer outText = new StringBuffer(); 
    int currentIndex = 0; 
    String tmp = textTemplate; 
    while(!tmp.isEmpty()) { 
     while(tmp.indexOf(" ") == 0) { 
      outText.append(tmpTextCandidate.charAt(currentIndex)); 
      tmp = tmp.substring(1); 
      currentIndex++; 
     } 
     if(!(tmp.charAt(0) == tmpTextCandidate.charAt(currentIndex))) { //This is in case you want both inputs "15:00" as well as the "1500" to output "15:00" 
      outText.append(tmp.charAt(0)); 
     } 
     tmp = tmp.substring(1); 
    } 
    while(currentIndex < tmpTextCandidate.length()) { 
     outText.append(tmpTextCandidate.charAt(currentIndex++)); 
    } 
    return outText.toString(); 
} 

請注意,這不是最佳的解決方案,並還可以,tWords參數沒有在任何地方使用的(因此我)實現了doCandidateWithTextTemplate功能。

+0

感謝您的響應,可以發佈更正後的代碼? –

+0

我現在正在工作,如果你自己調試,我想你會學到更多。但是如果您仍然遇到問題,我會在大約三個小時後再回來查看並正確執行。 – NitroNbg

+0

如果你發佈它會很好,謝謝 –