Java子字符串正在将我的正索引与负索引切换
我得出的结论是启用 HTML 的 JTextPanes 不支持自动换行。所以我试图提供一种家庭酿造方法。完成后我会将其发布到网上。我可能没有最好的技术,但完成后应该会起作用。我的问题是由于一些疯狂(非常令人沮丧)的原因,当我将索引传递给子字符串命令时,它会将实际值切换为相同的值,但为负值,并抛出 java.lang.StringIndexOutOfBoundsException。但是当变量被发送到子字符串命令时,它肯定是正数。当我用变量替换值时,效果很好。我将不胜感激任何帮助。我会包括酸的。
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
}
}
}
I have come to the conclusion HTML enabled JTextPanes do not support word wrapping. So I am attempting to provide a home brew method. I will post it on the 'net once it is complete. I may not have the greatest technique but it should work when it's done. My problem is for some insane (very fustrating) reason when I pass an index to my substring command it switches the actual value with the same value but in negitive and throws a java.lang.StringIndexOutOfBoundsException. But when the variable is sent to the substring command it is positive for sure. When I substitute the variables for values it works fine. I would appreciate any help. I will include the soure.
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
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您执行最后的
substring
调用时,endint
的值为 0,该值大于起始索引,从而导致 StringIndexOutOfBoundsException。When you do the final
substring
call, the value ofendint
is 0, which is larger than the start index, resulting in a StringIndexOutOfBoundsException.