【发布时间】:2014-01-27 13:11:05
【问题描述】:
线程“main”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));
}
}
【问题讨论】:
标签: java substring indexoutofboundsexception