Reputation: 483
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.
Upvotes: 3
Views: 204
Reputation: 25337
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.*
Upvotes: 0
Reputation: 14544
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)) {
Upvotes: 4
Reputation: 11947
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)));
Upvotes: 4