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

public class Hangman
{
  public static void main (String[] args)
     {
     char userAnswer;    
    String[] dictionary = { "acorn", "actor", "album", "alien",
     "bagel", "basin", "beach", "brick",
     "cable", "chalk", "cloud", "crowd",
     "debug", "ditch", "drill", "drink",
     "eagle", "eight", "elbow", "extra",
     "fable", "feast", "fizzy", "flood",
     "giant", "glass", "gross", "grump",
     "happy", "hoist", "hover", "humor",
     "icing", "ideal", "igloo", "itchy",
     "joker", "jolly", "juice", "jumbo",
     "knack", "knead", "knife", "knock",
     "ledge", "lemon", "lever", "llama",
     "macho", "mango", "meter", "mouth",
     "nasty", "ninja", "noise", "nudge",
     "oasis", "offer", "olive", "organ",
     "paint", "party", "paste", "pizza",
     "quack", "quail", "queen", "quirk",
     "radio", "relax", "rhyme", "rival",
     "smoke", "snail", "space", "syrup",
     "table", "throw", "train", "tulip",
     "ulcer", "union", "unite", "untie",
     "valid", "video", "viper", "vowel",
     "wagon", "water", "weird", "write",
     "yeast", "young", "yours", "yummy",
     "zesty", "zippy", "zoned"};
    
    Random index= new Random();
         
         System.out.print("Welcome to CS7 Hangman! \n");
         System.out.println("Try to guess" + " the word " +
                            "one letter " + "at a time.");
         System.out.println("You are allowed 6 misses.");
         System.out.println(" _____");
         System.out.println("|    |");
         System.out.println("|");
         System.out.println("|");
         System.out.println("|");
         System.out.println("|");
         System.out.println("|");
         System.out.println("|");
         System.out.println("|_______");
         
         String hidden_word;
    
    hidden_word= dictionary[index.nextInt(dictionary.length)].toLowerCase();
    for (int line=0; line < hidden_word.length(); line++)
        {
             System.out.print("_ ");
        }
    
    Scanner keyboard= new Scanner(System.in);
    userAnswer= keyboard.nextLine().toLowerCase().charAt(0);
         
         
     }
  
  public static char[] CharacterCheck(char userAnswer, String[] dictionary, String hidden_word)
     {
       char[] correctLetters;
       //int mistakeCounter;
       String answer_check; 
       answer_check= correctLetters.charAt();
       
       for(int i= 0; i < hidden_word.length; i++)
       {
         if(hidden_word.charAt(i)== userAnswer)
         {
           correctLetters[i]=userAnswer;
         }
         
       }
       
       return correctLetters;
     }
}

I am trying to write a simple Hangman game for some practice. Currently, the program can display the hangman gallow's and the hidden word dashes through the use of a for loop and the Random class. However, I am trying to pass the user response to the CharacterCheck method, but I keep getting an error saying that the complier can not find the char array correctLetters or the String variable hidden_word. At first, I thought I had made a typo when I tried to use the String variable answer_check to hold the letter value (using CharAt) for the correctLetters array, so I added the [] in front of correctLetters, but I was proven wrong when I got a new error message (which was the only one) saying that there is a "class expected". I looked through my program for any misplaced brackets or extra brackets ({}) and there were none, so I figured that was not the problem. So I am stumped and any help would be appreciated. It doesn't have to be the solution, but I would like to be pointed in the right direction.

public static char[] CharacterCheck(char userAnswer, String[] dictionary, String hidden_word)
     {
       char[] correctLetters;
       //int mistakeCounter;
       String answer_check; 
       answer_check= correctLetters.charAt();
       
       for(int i= 0; i < hidden_word.length(); i++)
       {
         if(hidden_word.charAt(i)== userAnswer)
         {
           correctLetters[i]=userAnswer;
         }
         
       }       
       return correctLetters;
     }

hidden_word is missing bracklets on the method call
However I do not know what you try to achive with array correctLetters? After you call method CharacterCheck you declare this array, but you DO NOT initialize it! Then you try to use String method charAt(position_num), position number is missing there, on character array. That will ofcourse kick error.
Also you should re-thing the approche to the problem. Your random doesn't work, gallows will be showed but you can't display anything on it...

It's not finished. I have the code for the gallows whenever the player guesses wrong, but I have not put it in the program yet because I'm trying to let the program check a player's guess and return a response. I also haven't put in an if statement for when the player guesses wrong for that reason.

With the correctLetters array, I am trying to have the CharacterCheck method return the correctLetters array to the main method so that the program will know if the player's guess is correct or not.

This article has been dead for over six months. Start a new discussion instead.