我編寫了一個西班牙語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'時,會出現標題中指出的錯誤!我該如何解決這個問題?謝謝〜
請發佈完整的堆棧跟蹤。這告訴你(和我們)哪條線路導致錯誤。 –
A是開關採樣器上的選項字母,而不是不定式。 – keepcalmanddebate