3

I have to write a hangman homework assignment. We aren't allowed to use any char array and only use String methods to manipulate the string itself. A problem I'm running into is when I try to check the string builder for duplicates.

This is my hangman method

public void playHangMan(Scanner guessWord)
{
    int error = 6;
    String letter;
    boolean vali, dup;

    displayHangman();// call displayHangman method

    // get input, lower case input, and then validate in a loop
    do
    {
        letter = guessWord.nextLine();
        letter = lowerGuess(letter); // call lowerGuess method
        vali = letter.matches("[a-z]");

        dup = checkDup(letter); // call checkDup method

        if(vali == false)
        {
            System.out.print("Please enter only a SINGLE letter: ");
        }
        else if(dup == true)
        {
            System.out.print("Duplicated letter, please enter another: ");
        }

        if(dup == false && vali == true)
        {
            this.guessAns.append(letter);
        }

    }
    while(vali == false && dup == false);


}// end playHangman method

My duplicate method:

private boolean checkDup(String letter)
{
    int i;
    boolean dup = false;

    // check guessAns StringBuilder for duplicate letters
    for(i = 0; i <= this.guessAns.length() - 1 && dup == false; i++)
    {
        if(letter.equals(this.guessAns.charAt(i)))
        {
            dup = true;
        }
    }

    if(dup == true)
    {
        return true;
    }
    else
    {
        return false;
    }   
}// end checkDup method

The problem is that my checkDup method isn't finding any duplicates. I tried appending the letter a into my string builder and entering in the value a for letter, but still no luck. Can it be that letter.equals(this.guessAns.charAt(i)) is comparing a String to a Char and that's why my checkDup method is failing to find duplicates? Can someone explain a way that I can get around this? Any help will be greatly appreciated.

5
  • I would use a Set<Character> or String.indexOf(..). Commented Nov 3, 2013 at 4:40
  • Note that the bottom half of checkDup can be simplified to return dup;. Commented Nov 3, 2013 at 4:42
  • 2
    @PaulBellora .. which can itself be simplified by removing the dup variable. Commented Nov 3, 2013 at 4:42
  • @PaulBellora Thanks! You're right. Will definitely change that up. Commented Nov 3, 2013 at 4:43
  • @user2864740 has a point - you could replace dup = true; with return true; and put return false; at the end. +1 for a well-written question btw. Commented Nov 3, 2013 at 4:44

3 Answers 3

4

2 ways you could work around this:

letter.charAt(0) == guessAns.charAt(i);

The solution above is under the assumption that letter is a single char.

The other way is to invoke Character.toString on the char:

letter.equals(Character.toString(guessAns.charAt(i)));
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the quick reply. Your input really helped out.
4

Calling String.equals(char) will always return false, as Strings never compare as equal to Characters. Do this to compare them as characters:

if(letter.charAt(0) == this.guessAns.charAt(i)) {

Comments

0

There is another way of checking for duplicates: using a regex. The regex .*([a-z])\1.* will match any String with a repeated character between a and z. To go more general, .*(.)\1.* will match any String with any repeated character.

dup = letter.matches(".*([a-z])\\1.*");

How this works: . matches any character and* means 0 or more times, so .* means any amount of any character (it "consumes" any amount of characters). ( starts a group; ) ends a group. [a-z] matches any character between a to z. \1 is a backreference. Therefore, the regex looks for any character [a-z] which is immediately followed by the same character.

Note: If you are looking to see if the String contains the same character even when separated, then this should work: .*([a-z]).*\1.*

Comments

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.