0

I am writing a hangman program and one of the requirements to a hangman game is preventing the user from entering the same letter twice.

I have written the code for that, but the problem is every time I enter a letter it says it is already entered. I need to only say it when it is entered the second time. You guys have any suggestions? I've been trying to fix this for the past few hours, I figured I could ask on here to find some help. I already looked at another Stackoverflow question regarding something similar to this but all those solutions have the same result.

In Java, how can I determine if a char array contains a particular character?

I've tried something like this but it won't work either:

boolean contains = false;
for (char c : store) {
    if (c == character) {
        System.out.println("Already Entered");
        contains = true;
        break;
    }
}
if (contains) {
    // loop to top
    continue;
}

SECOND CLASS-

public void hangman(String word, int life) {

    KeyboardReader reader = new KeyboardReader();
    char[] letter = new char[word.length()];
    char[] store = new char[word.length()];
    
    String guess;
    int i = 0, tries = 0, incorrect = 0, count = 1, v = 0;
    while (i < word.length()) {
        letter[i] = '-';

     
  • Looks to me like your code is correct but not inserted at the right place : you test whether the letter is a duplicate *after* having inserted it in the array of already used letters, which is why it's always considered a duplicate – Aaron Oct 23 '20 at 12:07
  • @Aaron Can you please check if I have put it in the right place now? – Nick Ferri Oct 23 '20 at 12:09
  • 1
    Still not good. Look at the start of your `while(life> 0) {` loop : you print a message, read a character and immediately insert it into the `store` variable. At this point your code will already consider the letter a duplicate since it tests whether the character you've read is present in the `store` array – Aaron Oct 23 '20 at 12:11
  • @Aaron Ok, where do you recommend to put it? – Nick Ferri Oct 23 '20 at 12:28
  • It won't work after you've inserted the character into the `store` array. I'm sure you know it won't work before you've read the character, so that only leaves you with one spot : after you've read the character but before you've inserted it into the array. At this spot the code should work as expected. – Aaron Oct 23 '20 at 12:31
  • And I think what you lack to make solving this problem easy is a mental picture of the state of your program, i.e. what variables contain what value at which spot. You might want to try debugging your code with a step-by-step debugger, they can easily show you this and I'm sure it would prove helpful in the future – Aaron Oct 23 '20 at 12:34

2 Answers2

0

Since Java 1.5 the class String contains the method contains(). My idea is to collect all entered letters into a string variable and using above method:

// My code line
String letterAlreadyEntered = "";
// Your code line 
char character = reader.readLine().charAt(0);
// My code line
if (letterAlreadyEntered.contains("" + character) == true) {
  //Put code here what ever you want to do with existing letter
} else {
  letterAlreadyEntered += character;
}

In my opinion, this is an easier way to check for occurrences than in arrays, where you have to write your own check method.

Reporter
  • 3,897
  • 5
  • 33
  • 47
0

I would just use the String.contains() method:

    String aString = "abc";
    char aChar = 'a';
    return aString.contains(aChar + ""); 

To keep track of guessed letters you can use a StringBuffer, appending them using a StringBuffer.append() to append new letters (maintaining state) and use the StringBuffer.toString() method to get the String representation when you need to do the comparison above.

Rob Evans
  • 2,822
  • 1
  • 9
  • 15