简体   繁体   English

Java:for 循环使用字符串 charAt 方法崩溃

[英]Java: for loop crashing using string charAt method

I have a bug in this block of code.我在这段代码中有一个错误。 The debugger suggest it´s cause is this line of code char chr = getSecretWord.charAt(i);调试器表明它的原因是这行代码char chr = getSecretWord.charAt(i);

What this code does is look for a match between userInput and secretWord .这段代码的作用是寻找userInputsecretWord之间的匹配。 I have the for loop to go through the length of the secretWord letters one by one, and if there is a letter matching return true.我有一个for loop来一个一个遍历 secretWord 字母的长度,如果有匹配的字母,则返回 true。 If not, return false... but the program crashes when it is suppose to just return false... I guess it is something with this line, but do not know exactly what getSecretWord.charAt(i);如果没有,则返回 false...但是当它假设返回 false 时程序崩溃了...我想这与这一行有关,但不知道究竟是什么getSecretWord.charAt(i);

    private boolean isMatchingSecretWord(String userInput)
{
    String secretWord = "";
    String getSecretWord = getSecretWord();
    for (int i = 0; i <= getSecretWord.length();i++)
        {
        char chr = getSecretWord.charAt(i);
        secretWord = ""+chr;

        if (secretWord.equals(userInput))
        {
            println("is true");
            return true;
        }
    }
    return false;
}

As an side note, is what I´ve done with this code correct, assigning the getSecretWorld() Method to a String so I can use the Strings method length() ?作为旁注,我对这段代码所做的是否正确,将 getSecretWorld() 方法分配给 String 以便我可以使用 Strings 方法length()

String getSecretWord = getSecretWord();

for (int i = 0; i <= getSecretWord.length();i++)

Debug code:调试代码:

Exception in thread "Thread-4" java.lang.StringIndexOutOfBoundsException: String index out of range: 4    
    at java.lang.String.charAt(String.java:686)    
    at Hangman.isMatchingSecretWord(Hangman.java:49)    
    at Hangman.userInput(Hangman.java:34)    
    at Hangman.run(Hangman.java:20)*
for (int i = 0; i <= getSecretWord.length(); i++)

should be:应该:

for (int i = 0; i < getSecretWord.length(); i++)
//               ^^^
//             see here

The valid indexes for an n -character string (or an n -element array) are 0 through n-1 inclusive. n字符字符串(或n元素数组)的有效索引为0n-1含)。

So, if your secret word is xyyzy , the valid indexes are zero through four.因此,如果您的秘密词是xyyzy ,则有效索引为 0 到 4。 Your original loop iterates with i set to zero through five, hence the problem.您的原始循环将i设置为 0 到5 进行迭代因此存在问题。


But there seems to be a lot of unnecessary code in there, when you could get away with something simple.但是当您可以通过一些简单的事情逃脱时,那里似乎有很多不必要的代码。

First, I would remove a source of confusion - the function name sounds like the user input and the secret word have to match completely whereas your comment indicates otherwise:首先,我会消除混淆的来源 - 函数名称听起来像用户输入和秘密词必须完全匹配,而您的评论则另有说明:

Thanks, this works.谢谢,这有效。 But the reason for the loops is that the user enters one letter, I want to see if that letter is within the SecretWord.但是循环的原因是用户输入了一个字母,我想看看那个字母是否在 SecretWord 中。 (it´sa hangman game). (这是一个刽子手游戏)。

In that case, you simply want to see if the single character exists in the secret word.在这种情况下,您只想查看秘密单词中是否存在单个字符。 I would change the function name to suit and, even then, it can be done with a lot less code:我会更改函数名称以适应,即使那样,也可以用更少的代码来完成:

private boolean isInSecretWord (String userInput) {
    String secretWord = getSecretWord();
    return secretWord.contains(userInput);
}

You were getting out of bounds error as your for loop wasn't looping correctly, I have modified it so that the loop doesn't go out of bounds and also your secretWord variable wasn't populating correctly, the code should now work as intended :)由于您的 for 循环没有正确循环,您遇到了越界错误,我对其进行了修改,以便循环不会越界,并且您的 secretWord 变量没有正确填充,代码现在应该按预期工作:)

private boolean isMatchingSecretWord(String userInput)
{
    String secretWord = "";
    String getSecretWord = getSecretWord();
    for (int i = 0; i < getSecretWord.length();i++)
        {
        char chr = getSecretWord.charAt(i);
        secretWord = secretWord + chr;

        if (secretWord.equals(userInput))
        {
            println("is true");
            return true;
        }
    }
    return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM