aspose file tools*
The moose likes Beginning Java and the fly likes Boolean.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Boolean...." Watch "Boolean...." New topic
Author

Boolean....

Kate ryan
Greenhorn

Joined: Dec 08, 2004
Posts: 5
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

Joined: Mar 13, 2004
Posts: 1272
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.


Mike Gershman
SCJP 1.4, SCWCD in process
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4745
    
    7

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.


Junilu - [How to Ask Questions] [How to Answer Questions]
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
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

Joined: Apr 04, 2004
Posts: 815
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.


I've heard it takes forever to grow a woman from the ground
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Boolean....