Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何修复java.lang.StringIndexOutOfBoundsException?_Java_Indexoutofboundsexception - Fatal编程技术网

如何修复java.lang.StringIndexOutOfBoundsException?

如何修复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

我正在编写一个hangman(游戏)程序,我有一个编码的短语,它以星号的形式显示给用户。当用户猜到一个正确的字母时,我尝试更改编码的星号短语,以便将一个星号更改为用户输入的字母。我使用的是indexOf方法,但它不断输出a-1并给出

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()
调用,因为字符串已经是大写的,一劳永逸。

如果我理解正确,您的短语是大写的

在检查userguess
if(phrase.contains(userguess.toUpperCase())
时,您将其转换为大写,但在检查索引
int index=phrase.indexOf(userguess)时你不是


在将userGuess转换为大写后尝试获取索引,就像在if条件下一样。

如果我理解正确,您的短语是大写的

在检查userguess
if(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
。尼思,我已经回答过了。