I've been trying to make a hangman game for a high school project but lack of experience and skills prevents me from getting it to work This is probably easy for you all but i really need this help bec i dont know whats wrong with my code.
Menu:
package hangnerd; import javax.swing.*; /** * * @author Jonathan */ public class GameMenu { HangGame hg = new HangGame(); /** * @param args the command line arguments */ public static void main(String[] args) { GameMenu gameMenu = new GameMenu(); } public GameMenu() { char opt =' '; do { menu(); String choice = JOptionPane.showInputDialog("Enter an option"); opt = choice.toUpperCase().charAt(0); options(opt); } while (opt != 'Q'); } final void menu() { System.out.println("A. Play Game"); System.out.println("Q. Quit"); } void options(char opt) { switch(opt) { case 'A':hg.PlayGame(); break; } } }
Second classpackage hangnerd; import java.util.*; import java.io.*; /** * * @author Jonathan */ public class WrdArray { WrdObj[] wordArr = new WrdObj[200]; int count = 0; public void FileToArray() throws IOException { try{ BufferedReader fr = new BufferedReader(new FileReader("Words.txt")); String line = fr.readLine(); while(line != null) { StringTokenizer stk = new StringTokenizer(line,"#"); String word = stk.nextToken(); String hint = stk.nextToken(); wordArr[count] = new WrdObj(word,hint); count++; line = fr.readLine(); } } catch (FileNotFoundException fnfe) { System.out.println("File not found"); } } public String RandomWrdGen() { int pos = (int) (Math.random()*count); String gameWrd = wordArr[pos].toString(); return gameWrd; } public void Display() { System.out.println("URarray=" + Arrays.toString(wordArr)); } public String toString() { String temp = ""; for (int i = 0; i < count; i++) { temp += wordArr[i]+"\n"; } return temp; } }
thrid class
package hangnerd; /** * * @author Jonathan */ public class WrdObj { private String word; private String hint; public WrdObj() { } public WrdObj (String w,String h) { word = w; hint = h; } public String getWord() { return word; } public String getHint() { return hint; } public void setWord(String w) { word = w; } public void setHint(String h) { hint = h; } public String toString() { String temp; temp = word + " " + hint; return temp; } }
fourth class
It will compile and I will get the chance to type in an option for my menu but after I input the letter A I get this message. When I input Q that part works.package hangnerd; import javax.swing.*; /** * * @author Jonathan */ public class HangGame { WrdArray wrd = new WrdArray(); public void PlayGame() { String gWrd = wrd.RandomWrdGen(); char newwrd[] = new char [100]; for (int i = 0 ; i < gWrd.length () ; i++) { newwrd [i] = '-'; } System.out.println (newwrd); int error = 0; int correct = 0; while (error != 5 && correct != gWrd.length ()) { boolean flag = false; char guess = JOptionPane.showInputDialog ("Guess a letter").charAt (0); for (int o = 0 ; o < gWrd.length () ; o++) { if (guess == gWrd.charAt (o)) { newwrd [o] = guess; correct++; flag = true; } } if (flag == false) { error++; } System.out.println (newwrd); } if (correct == gWrd.length ()) { JOptionPane.showMessageDialog (null, "You win!"); } else { JOptionPane.showMessageDialog (null, "You lose!"); } } }
These are the errors:
Originally Posted by output