-2
即时尝试使一个方法返回短语中的字母数为n个字母。我不断收到字符串索引超出范围:-1错误索引超出范围错误-1(==和!=)
public static int nCount(String phrase, int n){
String phrase3 = phrase;
int phrase3Length = phrase3.length();
int counter = 0;
int currentWordLength = 0;
int i = 0; //words checked
int numberOfWords = words(phrase); //already have a method that checks for # of words
while (numberOfWords > i) {
while (phrase3.indexOf(" ") != 0) {
phrase3 = phrase3.substring(1); //line of trouble!! (index out of range -1)
currentWordLength++;
}
while (phrase3.indexOf(" ") == 0) {
phrase3 = phrase3.substring(1);
}
if (currentWordLength == n) {
counter++;
i++;
currentWordLength = 0;
} else {
i++;
currentWordLength = 0;
}
}
请勿转述错误消息。完全复制它。 – 2014-10-20 02:43:13
为什么不与我们分享错误的堆栈跟踪?(这是我的另一个$ 1“我得到和错误,但我不包括它”基金) – John3136 2014-10-20 02:43:28
phrase3 = phrase3.substring(1);你可以expalin这条线吗? – 2014-10-20 02:44:10