2011-10-08 28 views
2

我已經得出結論啓用HTML的JTextPanes不支持文字換行。所以我試圖提供一種家庭釀造方法。一旦完成,我會在網上發佈它。我可能沒有最好的技術,但它應該在完成時工作。我的問題是一些瘋狂(非常鬱悶)的原因,當我通過一個索引到我的子字符串命令它切換實際值與相同的值,但在否定和拋出java.lang.StringIndexOutOfBoundsException。但是當變量被髮送到子串命令時肯定是肯定的。當我將變量替換爲值時,它工作正常。我將不勝感激任何幫助。我將包括soure。Java子串將我的積極索引切換爲負數

String wordWrap(String oldtxt) { 

     int ishere = 0;   // the current index 
     int charlen = 0;  // The current length of the current line 
     int beginint = 0;  // Where the text between tags begins 
     int endint = 0;   // Where the text between tags ends 
     String tempstr = ""; // Where the text is 
     String newoldtxt = ""; // to work around the damned oc error 
     String newtxt = "";  // The new text being built 
     String divystr = ""; // Temp variable to hold a partial string 
     Boolean a = true;  // Needed as temp storage variable 

     newoldtxt = oldtxt; 

     while(true) { 

      endint = oldtxt.indexOf("<", endint); 

      if(endint == -1) { 

       endint = oldtxt.length(); 
       a = false; 
      } 


      ishere = endint; 
      tempstr = oldtxt.substring(beginint, endint);    // Save the text in a temp string 

      while(ishere > endint) 
      if(tempstr.length() > (22 - charlen)) {     // Testing for a complete line 

//    newtxt += tempstr.substring(ishere, 22 - charlen);  // If a line is complete a line is printed to the pane 
       newtxt += tempstr.substring(ishere, 22);  // If a line is complete a line is printed to the pane 
       ishere += 22 - charlen;          // Bumping the current index along 
       charlen = 0; 
       newtxt += "<br />";           // Attach a line break 
       if(ishere >= tempstr.length() && a == false) 
       return newtxt; 

      } else if(tempstr.length() < (22 - charlen)) {   // Checking to see if there are fewer then 22 chars in the string 

       divystr = tempstr.substring(ishere, tempstr.length()); // Dump the rest of the substring into the rebuilt string 
       newtxt += divystr;           // Assign the remaining tempstr characters to newtxt 
       charlen += divystr.length();        // Add stray chars to charlen 

       if(a == false) 
        return newtxt; 
      } 

      beginint = oldtxt.indexOf(">", (endint));      // Locate end bracke 
      newtxt += oldtxt.substring(beginint, endint);    // Add tag to newtxt 
     } 
    } 
} 
+0

嗨,羅蘭,歡迎來到SO! – Ben

+0

感謝史蒂夫,不錯的網站 –

+0

@Roland Sams寫了'不支持文字包裝'我認爲那根本不是真的 – mKorbel

回答

1

當你做最後的substring通話中,endint值是0,這不是開始指數越大,造成的StringIndexOutOfBoundsException。

// endint is larger than beginint here 
newtxt += oldtxt.substring(beginint, endint); 
+0

謝謝,但我已經嘗試過了。我仍然遇到了出界錯誤。此代碼尚未發佈代碼。我還沒有機會去追趕這些臭蟲,因爲這個令人討厭的故障 –

+0

你用什麼作爲輸入數據? 'endint'的值由'oldtxt'中第一個'<'符號的位置決定。 – fivedigit

+0

順便說一句:越界異常-21實際指數應該是21.我certian變量發送索引是21由於某種原因子字符串方法扭轉它。 –