Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Boolean....

 
Kate ryan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
writing a hangman program, but it doesn't seem to be returning the boolean values correctly?? Any help, v. much appreciated!
Thanks!


for(int i = 0; i < 20 ; i++)
{
boolean found = false;
while(found !=true)
{
for (int a = 0; a < word.length; a++)
{
if (attempt == word[a])
{
dashes[a] = (char)attempt;
rightGuess++;
}
}

if(rightGuess==0)
{
wrongGuess++;
found = false;
}
else
found = true;
}

if (found == false)
System.out.println("Wrong");
else
System.out.println("Right");


System.out.print("Enter Guess: ");
for(int i=0; i<letters.length; i++)
System.out.print(dashes[i]);
System.out.println();
System.out.print("Enter Guess : ");
attempt = Console.readChar();

}
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I expect that "if (attempt == word[a])" should be
"if (attempt.equals( word[a] ) )"

You should be checking for 2 strings with the same character sequence, but you are actually testing whether these are literally the same String object.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kate,

Welcome to JavaRanch!

Please don't post the same question to more than one forum -- it just wastes people's time. Thanks.

I deleted the other copies of this thread, since this one has an answer.
 
Junilu Lacar
Bartender
Pie
Posts: 7306
45
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mike Gershman:
I expect that "if (attempt == word[a])" should be
"if (attempt.equals( word[a] ) )"


I don't know about that. From what I can tell from the posted code, "attempt" is a char and "word" is a char[]. In that case, (attempt == word[a]) should work and (attempt.equals(word[a])) would fail.

Kate, post the actual code and/or error or erroneous output.
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu said:
From what I can tell from the posted code, "attempt" is a char and "word" is a char[].

I see ... I agree

Based on Junilu's theory, the only error I see is the while loop which never ends if the attempt is wrong. Just eliminate it and add "!found" to the for loop continuation condition.
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also worth noting:




can be written


found==false returns a boolean. No need to go through the extra step if you already have a boolean.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic