2016-03-20 162 views
0

該代碼編譯並適用於任何傳遞的字符串,但它不適用於檢查元音。它拋出一個字符串越界錯誤,我不知道爲什麼。輔音的檢查工作。獲取字符串越界異常

下面是代碼:

public String catchword(String word){ 
    int x = 0; 
    for(x=0; x<word.length()+1; x++){ 
     boolean v = Vowel(word.charAt(x)); 
     boolean c = Consonant(word.charAt(x)); 
     if (x<word.length()-1){ 
      v = Vowel(word.charAt(x+1)); 
     } else{ 
      v = true; 
     } 

     if (c == true && v == true){ 
      word = word.substring(0,x+1) + "op" + word.substring(x+1,word.length()); 
      x = x+3; 
     } 
    } 
    System.out.print(word); 
    return word; 
} 
+2

'for(x = 0; x Tunaki

+0

啊。我現在看到了這個問題。謝謝 – john

回答

0

for(x=0; x<word.length()+1; x++)

x<word.length()+1 

應該

x<word.length()-1 

word的最大指數是word.length()-1

您在word.charAt(x+1)也會遇到問題。 word.length() - 1是最大指數,當x將等於它,你會再次得到IndexOutOfBoundsException

+0

謝謝!它現在正在工作 – john