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.