This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
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 Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Boolean...." Watch "Boolean...." New topic


Kate ryan

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!

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;

found = false;
found = true;

if (found == false)

System.out.print("Enter Guess: ");
for(int i=0; i<letters.length; i++)
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

Joined: Jul 08, 2003
Posts: 24199

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

Joined: Feb 26, 2001
Posts: 6529

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
I agree. Here's the link:
subject: Boolean....
It's not a secret anymore!