2

I'm programming a traditional hangman game in Java. What I'm currently stuck on is to find if the users character input is not a character within the String.

if(getLetters.indexOf(userCharInput)==-1) //getLetters is the StringBuilder, and the userCharInput is a String.
{
     playerCounter++;
}

This is the section that I seem to have trouble in, I've looked at different indexOf examples and I've formulated this to work with my program.

The problem is, It doesn't work. I set it so the player has 3 chances to guess the word, since the default word is "apple" I guessed 'a', 'p', and 'l', which leaves 'e' to be guessed. Now I intentionally make 3 incorrect guesses and it doesn't proc the next else if:

else if(playerCounter == 3)
{
    System.out.println("All lives are gone! Game Over!");
    playerCounter = 1; //resets the playerCounter to one.
    System.exit(0);
}

Any help will be greatly appreciated.

3
  • 1
    can you provide complete program. Did u try to debug it? Commented Dec 31, 2012 at 22:50
  • 1
    Put else if within your if(getLetters.indexOf(userCharInput)==-1). Commented Dec 31, 2012 at 22:53
  • @Shivam Kalra I tried that and it still having the same problem. Commented Dec 31, 2012 at 23:46

4 Answers 4

3

It's because when you keep guessing the wrong letter, the first if statement is evaluated to true:

if(getLetters.indexOf(userCharInput)==-1) //getLetters is the StringBuilder, and the userCharInput is a String.
{
     playerCounter++;
}

So you keep increasing playerCounter (beyond 3). This means that your next statement is unreachable (once it's greater than 3, it's not going to get any smaller, at least with the code you have posted so far). So else if (playerCounter == 3) may not be reachable.

Sign up to request clarification or add additional context in comments.

3 Comments

Yeah you're right, fixed everything up but I'm still having the same problem.
Please post an SSCCE which illustrates your current problem.
This was half of the reason why it wasn't working but was the closest to the solution I came across, with the help of @paulsm4.
1

I guess that your else if is part of another if statement that is getting true, so check that out.

1 Comment

It might have been that, but I did what @Shivam Kalra said and it still doesn't seem to work.
0

The reason it was not working was because of the indexOf method being used improperly,it was partially because of the ordering of the if statements, but that was very miniscule to the primary issue. What had to be changed was the way I used the indexOf method.

ie. instead of gletter.indexOf(character); it should have been word.indexOf(character); where word was the word that had to be guessed, and gletter was the StringBuilder used to keep track of user guesses.

Comments

-2

Here's the Javadoc for StringBuilder:

Based on the code you've posted, I'm guessing:

1) You're saving the letters needed (e.g. the letters in the word "apple") to getLetters.

2) getLetters is type StringBuilder

3) if userCharInput is type "char", then indexOf() probably won't give the expected results

4) Substitute "if" for your "else if"

SUGGESTIONS:

Try changing "getLetters" to a "String", and see if that helps.

while (...) {
  ...
  //getLetters is the StringBuilder, and the userCharInput is a String.
  if(getLetters.indexOf(userCharInput)==-1)  {
     playerCounter++;
  }
  // Debug: comment this out once it's working
  System.out.println ("userCharInput=" + userCharInput + ", playerCounter=" + playerCounter);
  if(playerCounter == 3) {
    System.out.println("All lives are gone! Game Over!");
    playerCounter = 1; //resets the playerCounter to one.
    System.exit(0);
  }
  ...
}

8 Comments

userCharInput is a String according to the comment in the code at the top of this post
I didn't see it because the stupid comment scrolled off the right. Nevertheless - your answer is correct. It won't fail until after playerCounter >= 3 and he gives at least one correct letter. I've updated my post.
I tried putting the second if that you have withing the first since it seemed a bit simpler, it's not working, the only thing different from this solution to what I started off with was that it was an else if, I'll try your solution and get back to you.
And if course make sure that "playerCount" is incrementing in the first place. I'll make one more update to the code, to help you verify this (if you're not already using a debugger)...
Okay, I tried your solution and it still doesn't work, for now I think it's safer to make sure it check if the counter reaches 3 withing the first if statement. Still wondering why this isn't working, and I can only presume that if(getLetters.indexOf(userCharInput)==-1) { playerCounter++; } is incrementing in the first place.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.