2014-01-13 32 views
0

我編寫了一個西班牙語Conjugator。例如,如果您單擊方法isItEr(),則輸入單詞,然後在開關採樣器中輸入與表單關聯的字母。爲什麼我收到字符串索引超出範圍:97?

該錯誤是上的線 '情況下的:' java.lang.StringIndexOutOfBoundsException: 字符串索引超出範圍:97(在java.lang.String中)

public void isItIr() 
{ 
    System.out.print("Enter the infinitive: "); 
    Scanner keyIn = new Scanner(System.in); 
    String infinitive = keyIn.nextLine().toLowerCase(); 
    if(infinitive.substring(infinitive.length()-2, infinitive.length()).equals("ir")) 
      word = conjugateEndingIr(infinitive); 
} 

private String conjugateEndingIr(String infinitive) 
{ 
    Scanner keyIn = new Scanner(System.in); 

    System.out.println("Choose form: "); 

    System.out.println("a. Yo"); 
    System.out.println("b. Tu"); 
    System.out.println("c. El/Ella/Usted"); 
    System.out.println("d. Nosotros"); 
    System.out.println("e. Ellos/Ellas/Ustedes"); 
    System.out.println("f. Exit program"); 

    System.out.print("Enter Choice => "); 
    String input = keyIn.nextLine().toLowerCase(); 
    char ch = input.charAt(0); 

    if (!(ch >= 'a' && ch <='e')) 
    { 
     System.out.println("Invalid Choice!!!"); 
     System.out.print("Please enter choice again => "); 
     input = keyIn.nextLine().toLowerCase(); 
     ch = input.charAt(0); 
    } 

    switch(ch) 
    { 
     case 'a': 
     char idxio = 'a'; 
     word = infinitive.substring(0, idxio) + "o"; 
     return word; 
     case 'b': 
     char idxesi = 'a'; 
     word = infinitive.substring(0, idxesi) + "es"; 
     return word; 
     case 'c': 
     char idxei = 'a'; 
     word = infinitive.substring(0, idxei) + "e"; 
     return word; 
     case 'd': 
     char idximos = 'a'; 
     word = infinitive.substring(0, idximos) + "imos"; 
     return word; 
     case 'e': 
     char idxeni = 'a'; 
     word = infinitive.substring(0, idxeni) + "en"; 
     return word; 
     case 'f': 
     System.out.println("Goodbye"); 
     System.exit(-1); 
     break; 
     default: 
     System.out.println("Invalid menu choice"); 
     System.out.println("Goodbye"); 
     System.exit(-1); 
    } 
    return word; 
} 

`

當我輸入beber時,它會將開關採樣器加載到「選擇表單」中,當我放入'a'時,會出現標題中指出的錯誤!我該如何解決這個問題?謝謝〜

+0

請發佈完整的堆棧跟蹤。這告訴你(和我們)哪條線路導致錯誤。 –

+0

A是開關採樣器上的選項字母,而不是不定式。 – keepcalmanddebate

回答

1

這是因爲'a'是您的代碼重新解釋爲代碼點(恰好是97)的字符。

當你做到這一點

char idxio = 'a'; 
word = infinitive.substring(0, idxio) + "o"; 

串試圖以第97個字符是一個字短於97

什麼你可能想的是找到信'a'的指數從目標詞的末尾開始,切斷結尾,並用'o'替換它。您可以使用lastIndexOf('a')爲:

int idxio = infinitive.lastIndexOf('a'); 
word = infinitive.substring(0, idxio) + "o"; 
+0

是的,我的確打算這樣做。我需要做什麼改變? – keepcalmanddebate

+1

@ user2994631請參閱答案的最後兩行。 – dasblinkenlight

+0

它說它找不到符號 - 方法lastIndexOf(char)。我的代碼中沒有這種方法,但我知道你在哪裏。 – keepcalmanddebate

0

不要使用String.substring。如果您查看String類的Javadoc,您將看到boolean endsWith(String suffix)方法。使用最自然和具體的方法。

您可能還想使用正則表達式(.*)ir$;如果匹配,被捕獲的組是基礎。在西班牙語中,你能否從詞本身確定詞綴的形式,而不是讓用戶介入?

+1

這會做出一個完美的評論,而不是一個答案,因爲它解釋了OP如何優化已經工作的內容(可能效率並不高),但它不能告訴OP如何解決他的問題。 – dasblinkenlight

+0

不,因爲beber用戶可以用5種不同的方式共軛,只要用'beber' 好吧,那麼你的意思是把它放在什麼地方?如果是? – keepcalmanddebate

+0

我想你是對的。'endsWith'將替換'isItIr'的第四行中的子串代碼。 –