Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8541585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:51:54+00:00 2026-06-11T11:51:54+00:00

I have a bug in this block of code. The debugger suggest it´s cause

  • 0

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);

What this code does is look for a match between userInput and secretWord. 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. 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);

    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()?

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)*
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:51:55+00:00Added an answer on June 11, 2026 at 11:51 am
    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.

    So, if your secret word is xyyzy, the valid indexes are zero through four. Your original loop iterates with i set to zero through five, hence the problem.


    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. (it´s a 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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have this bug that only appears 30% of the time for the Release
Can you help de-bug this program. I have it almost perfect but when I
I have recently discovered that I am affected by this bug http://www.mail-archive.com/mono-bugs@lists.ximian.com/msg71515.html Well, at
I have some data, like this: BUG DATE STATUS ---- ---------------------- -------- 9012 18/03/2008
I have this supper weird bug, where this method fails in IE9. What is
This is the weirdest bug! It is probably something silly, but I have no
I have this code, and for some reason my $.post function fires twice in
This may be the most obscure bug I have yet encountered in many years
Ok so this bug is very weird here is my code, you can actually
I have completed my first application and debugger shows no issue in my code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.