在線程異常「主」 java.lang.StringIndexOutOfBoundsException:字符串索引超出範圍:-60異常在線程「主」 java.lang.StringIndexOutOfBoundsException:字符串索引超出範圍:-60
我不斷得到這個錯誤,我一直試圖弄清楚,但我不能!我剛剛開始使用Java,因此非常感謝所有幫助!這裏是我的代碼:
//This method takes large amounts of text and formats
//them nicely in equal lenth lines for the console.
public void print(String a){
String textLine = a;
int x = 60;
List<String> splitText = new ArrayList<String>();
//limits the amount of characters in a printed line to 60 + the next word.
while (textLine.length() > 60) {
if (textLine.substring(x+1,1) == " "){
splitText.add(textLine.substring(0,x+1));
textLine = textLine.substring(x+2);
x = 0;
}
else {
x++;
}
}
splitText.add(textLine);
for (int y = 0; splitText.size() < y;y++){
System.out.println(splitText.get(y));
}
}
你也可能需要閱讀http://stackoverflow.com/questions/513832/how -do-i-compare-strings-in-java –