0
import java.util.Scanner;
import java.util.Random;

public class HangmanGame2 
{
    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in);
        final String[] WordList = { 
                                                "porcupine", 
                                                "flamingo", 
                                                "sasquatch", 
                                                "poseidon", 
                                                "minnie mouse", 
                                                "mickey mouse", 
                                                "galileo" 
                                            };
        String ChosenWord = WordList[(int)Math.random()*WordList.length];
        StringBuffer display = new StringBuffer(ChosenWord.length());

        for (int i = 0; i < ChosenWord.length(); i++)
            display.append("*");

        int NumberOfTries = 0;

        System.out.print("Let's Begin \n\n");
        System.out.println("Instructions: Enter a letter when asked. Try to guess the word in less than 6 tries or you will be a Dead Man! Good luck!\n\n");

        boolean correct = false;

        while (NumberOfTries < 6 && !correct)
        {
            String UserGuess = input.next();
            String Letter = UserGuess.substring(0,1);

            if (ChosenWord.indexOf(Letter) < 0)
            {
            System.out.printf("The letter %s does not appear anywhere in the word.\n",Letter);
            NumberOfTries++;
            }

            else 
            {
                if (display.indexOf(Letter) >= 0)
                System.out.printf("The letter %s has already been entered as a guess.\n",Letter);

            else 
            {
                for (int p = 0; p < ChosenWord.length(); p++)
                if (ChosenWord.CharAt(p)= Letter.CharAt(0))
                display.setCharAt(p, Letter.CharAt(0));
            }
            }

            correct = display.indexOf("*") < 0;
            draw(NumberOfTries);
        }

        if (correct)
        System.out.println("You have guessed " + ChosenWord + " correct and saved yourself from the gallos. Till next time that is.\n");

        else
        {
        System.out.printf("You've had %d strikes against you. Thus you've been hung. Better luck next time.\n", NumberOfTries);
        }
    }

    public static void draw (int num) 
    {
        final String[] status = {
                                            "____\n| |\n|\n|\n|",
                                            "____\n| |\n|O\n|\n|\n|",
                                            "____\n| |\n|O\n|/|\n|\n|",
                                            "____\n| |\n|O\n|/|\\\n|\n|",
                                            "____\n| |\n|O\n|/|\\\n|/\n|",
                                            "____\n| |\n|O\n|/|\\\n|/\\\n|"
                                        };

        if (num >= 0 && num < status.length)
        {
            System.out.println(status(num));
        }

        else
        {
            System.out.println("Must be a Mistake. Out of Range.");
        }
    }
}

I currently have a couple errors still with my program. Can anyone help me figure out how to fix these? The errors are as follows and the compleate program is posted above.

HangmanGame2.java:50: error: cannot find symbol
                if (ChosenWord.CharAt(p)= Letter.CharAt(0))
                              ^
  symbol:   method CharAt(int)
  location: variable ChosenWord of type String
HangmanGame2.java:50: error: cannot find symbol
                if (ChosenWord.CharAt(p)= Letter.CharAt(0))
                                                ^
  symbol:   method CharAt(int)
  location: variable Letter of type String
HangmanGame2.java:51: error: cannot find symbol
                display.setCharAt(p, Letter.CharAt(0));
                                           ^
  symbol:   method CharAt(int)
  location: variable Letter of type String
HangmanGame2.java:81: error: cannot find symbol
            System.out.println(status(num));
                               ^
  symbol:   method status(int)
  location: class HangmanGame2
4 errors

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.
2
Contributors
1
Reply
16
Views
3 Years
Discussion Span
Last Post by IIM
1

method name are case sensitive,it is charAt() not CharAt.Line 51 and 52 .also if condition must be boolean not integer.So use == operator

 if (ChosenWord.charAt(p)== Letter.charAt(0))
                display.setCharAt(p, Letter.charAt(0));

in line 52 ,use proper syntax of array.it must be square brackets

System.out.println(status[num]);

Edited by IIM

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.