如何修复java.lang.StringIndexOutOfBoundsException?
我正在编写一个hangman(游戏)程序,我有一个编码的短语,它以星号的形式显示给用户。当用户猜到一个正确的字母时,我尝试更改编码的星号短语,以便将一个星号更改为用户输入的字母。我使用的是indexOf方法,但它不断输出a-1并给出如何修复java.lang.StringIndexOutOfBoundsException?,java,indexoutofboundsexception,Java,Indexoutofboundsexception,我正在编写一个hangman(游戏)程序,我有一个编码的短语,它以星号的形式显示给用户。当用户猜到一个正确的字母时,我尝试更改编码的星号短语,以便将一个星号更改为用户输入的字母。我使用的是indexOf方法,但它不断输出a-1并给出 java.lang.StringIndexOutOfBoundsException String index out of range -1 代码如下: System.out.print("Enter your next guess: "); String u
java.lang.StringIndexOutOfBoundsException
String index out of range -1
代码如下:
System.out.print("Enter your next guess: ");
String userGuess = keyboard.nextLine();
System.out.println("You guessed " + userGuess.toUpperCase() + ".");
System.out.println();
if(phrase.contains(userGuess.toUpperCase())) {
System.out.println("This is present in the secret phrase.");
System.out.println();
System.out.println("Number of wrong guesses so far: " + wrongGuesses);
int index = phrase.indexOf(userGuess);
System.out.print(index);
encodedPhrase = (encodedPhrase.substring(0, index) + userGuess + encodedPhrase.substring(index + 1));
根据您的陈述,
userGuess
可能不属于您的短语:
int index = phrase.indexOf(userGuess);
如果userGuess
不是短语的一部分,则返回-1。因此,在使用子字符串之前,请尝试使用:
if (index < 0) {
//userGuess not part of phrase
} else {
//do get substring and other business logic
}
根据您的陈述,userGuess
可能不属于您的短语:
int index = phrase.indexOf(userGuess);
如果userGuess
不是短语的一部分,则返回-1。因此,在使用子字符串之前,请尝试使用:
if (index < 0) {
//userGuess not part of phrase
} else {
//do get substring and other business logic
}
仅仅因为字符串包含userGuess.toUpperCase()
,并不意味着它也包含userGuess
。如果没有,你会得到-1
一个简单的解决方案:
String userGuess = keyboard.nextLine().toUpperCase();
然后您可以删除所有其他.toUpperCase()
调用,因为字符串已经是大写的,一劳永逸。仅仅因为字符串包含userGuess.toUpperCase()
,并不意味着它也包含userGuess
。如果没有,你会得到-1
一个简单的解决方案:
String userGuess = keyboard.nextLine().toUpperCase();
然后您可以删除所有其他.toUpperCase()
调用,因为字符串已经是大写的,一劳永逸。如果我理解正确,您的短语是大写的
在检查userguessif(phrase.contains(userguess.toUpperCase())
时,您将其转换为大写,但在检查索引int index=phrase.indexOf(userguess)时代码>你不是
在将userGuess转换为大写后尝试获取索引,就像在if条件下一样。如果我理解正确,您的短语是大写的
在检查userguessif(phrase.contains(userguess.toUpperCase())
时,您将其转换为大写,但在检查索引int index=phrase.indexOf(userguess)时代码>你不是
在将userGuess转换为大写后,尝试获取索引,就像在if条件中一样。您验证了用户猜测的大写版本在字符串中,但稍后您的indexOf()
检查没有检查大写版本。将用户的猜测转换为大写,然后检查它是否在字符串中以及它的索引是什么 您验证了用户猜测的大写版本在字符串中,但随后您的indexOf()
检查没有检查大写版本。将用户的猜测转换为大写,然后检查它是否在字符串中以及它的索引是什么 您必须将输入字符和单词转换为大写或小写
而不是:
phrase.contains(userGuess.toUpperCase())
写:
phrase.toUpperCase().contains(userGuess.toUpperCase())
您必须将输入字符和单词转换为大写或小写
而不是:
phrase.contains(userGuess.toUpperCase())
写:
phrase.toUpperCase().contains(userGuess.toUpperCase())
phrase.indexOf(userGuess)代码>返回-1,表示找不到它。将其更改为phrase.indexOf(userGuess.toUpperCase()代码>短语.indexOf(userGuess)代码>返回-1,表示找不到它。将其更改为phrase.indexOf(userGuess.toUpperCase()代码>我正在测试的短语是“Psycho”,每当我输入“P”时,我仍然会得到这个错误,即使它是字符串的一部分。@Hector您应该将字符串转换为大写,然后对大写字符串执行indexOf
。NRitH,我已经回答了。我测试的短语是“Psycho”,每当我输入“P”,我仍然会得到这个错误,即使它是字符串的一部分。@Hector你应该将字符串转换为大写,然后对大写字符串执行indexOf
。尼思,我已经回答过了。