# Java: Word Guessing Game



## LawOfPhysics (Nov 26, 2008)

I have two problems that I need help solving.

1. After the first "Enter a letter (! To guess the entire word, " + "1 to exit): " string prints out you input a letter. After that it prints out a set of dashes where the letter you have guessed is shown. It loops back to the string above and prints it out TWO times. I want it to print out once.

2. For the dashes and the correct letters I'm trying to use Stringbuffer. If I input the correct letter then input the next letter the first letter I inputted is not shown. It's hard to explain. Basically the correct letters don't appear in the dashes whenever the next letter is inputted.

Also, my code is REALLY huge and it could be condensed but the methods I try to use will cause massive errors. Thanks in advanced. I appreciate it a lot.

-A struggling Java student .


```
import learning.LearningIO;
import learning.*;

class Exercise9 {

   public static void main(String[] args) {
       String word = "- - - - -", guess, rightWord = "BRAIN";
       int compare, count = 0, countWins = 0, countLosses = 0;
           
       System.out.println("Word Guessing Game" + "\n" + word);
       
       for (;;) {
       StringBuffer strbuf = new StringBuffer();
       strbuf.append(word);
           
       System.out.println("Enter a letter (! To guess the entire word, " +
                        "1 to exit): ");
       char letter = LearningIO.readChar();
       
       if (letter == '1')
                        {
                        System.out.println("You have chosen to exit.");
                        break;
                        }
       else if (letter=='c' || letter=='d' || letter=='e' || letter=='f' || 
           letter=='g' || letter=='h' || letter=='j' || letter=='k' || 
                letter=='l' || letter=='m' || 
           letter=='o' || letter=='p' || letter=='q' || letter=='s' 
                || letter=='t' || 
           letter=='u' || letter=='v' || letter=='w' || letter=='x' 
                || letter=='y' || letter=='z' || letter=='C' || letter=='D' || letter=='E' || letter=='F' || 
           letter=='G' || letter=='H' || letter=='J' || letter=='K' || 
                letter=='L' || letter=='M' || 
           letter=='O' || letter=='P' || letter=='Q' || letter=='S' 
                || letter=='T' || 
           letter=='U' || letter=='V' || letter=='W' || letter=='X' 
                || letter=='Y' || letter=='Z') {
                    count++;
                    countLosses++;
                }
       else if (letter=='b' || letter=='r' || letter=='a' || letter=='i' || 
                letter=='n' || letter=='B' || letter=='R' || letter=='A' || letter=='I' || 
                letter=='N') {
                         count++;
                         countWins++;
                         if (letter=='b' || letter=='B'){
                             strbuf.setCharAt(0,'B');
                             System.out.println(strbuf);
                         }
                         if (letter=='r'){
                             strbuf.setCharAt(2,'R');
                             System.out.println(strbuf);
                         }
                         if (letter=='a'){
                             strbuf.setCharAt(4,'A');
                             System.out.println(strbuf);
                         }
                         if (letter=='i'){
                             strbuf.setCharAt(6,'I');
                             System.out.println(strbuf);
                         }
                         if (letter=='n'){
                             strbuf.setCharAt(8,'N');
                             System.out.println(strbuf);
                         }
                     }
       else if (letter == '!')
                    {
                    System.out.println("What's your guess?");
                    guess = LearningIO.readString();
                    ++count;
                    compare = guess.compareToIgnoreCase(rightWord);
       if (compare == 0)
             {
            countWins++;
            System.out.println("You won!" + "\n" + "The secret word is " + 
                               rightWord + "."
            + "\n" + "You made " + count + " guesses." + "\n" + "Wins: " + 
            countWins + "       " + "Losses: " + countLosses);
            break;
                }
       else {
           ++countLosses;
       }
            }
            if (countWins == 5)
                  {
                 System.out.println("You won!" + "\n" + "The secret word is " + 
                                    rightWord + "."
                 + "\n" + "You made " + count + " guesses." + "\n" + "Wins: " + 
                 countWins + "       " + "Losses: " + countLosses);
                 break;
                     }
        }
    }
}
```


----------

