Having Some Trouble with Methods

Reply

Join Date: Jun 2005
Posts: 32
Reputation: DaMoogle is an unknown quantity at this point 
Solved Threads: 0
DaMoogle DaMoogle is offline Offline
Light Poster

Having Some Trouble with Methods

 
0
  #1
Apr 15th, 2007
  1. import java.util.Scanner;
  2. import java.util.Random;
  3.  
  4. public class Hangman
  5. {
  6. public static void main (String[] args)
  7. {
  8. char userAnswer;
  9. String[] dictionary = { "acorn", "actor", "album", "alien",
  10. "bagel", "basin", "beach", "brick",
  11. "cable", "chalk", "cloud", "crowd",
  12. "debug", "ditch", "drill", "drink",
  13. "eagle", "eight", "elbow", "extra",
  14. "fable", "feast", "fizzy", "flood",
  15. "giant", "glass", "gross", "grump",
  16. "happy", "hoist", "hover", "humor",
  17. "icing", "ideal", "igloo", "itchy",
  18. "joker", "jolly", "juice", "jumbo",
  19. "knack", "knead", "knife", "knock",
  20. "ledge", "lemon", "lever", "llama",
  21. "macho", "mango", "meter", "mouth",
  22. "nasty", "ninja", "noise", "nudge",
  23. "oasis", "offer", "olive", "organ",
  24. "paint", "party", "paste", "pizza",
  25. "quack", "quail", "queen", "quirk",
  26. "radio", "relax", "rhyme", "rival",
  27. "smoke", "snail", "space", "syrup",
  28. "table", "throw", "train", "tulip",
  29. "ulcer", "union", "unite", "untie",
  30. "valid", "video", "viper", "vowel",
  31. "wagon", "water", "weird", "write",
  32. "yeast", "young", "yours", "yummy",
  33. "zesty", "zippy", "zoned"};
  34.  
  35. Random index= new Random();
  36.  
  37. System.out.print("Welcome to CS7 Hangman! \n");
  38. System.out.println("Try to guess" + " the word " +
  39. "one letter " + "at a time.");
  40. System.out.println("You are allowed 6 misses.");
  41. System.out.println(" _____");
  42. System.out.println("| |");
  43. System.out.println("|");
  44. System.out.println("|");
  45. System.out.println("|");
  46. System.out.println("|");
  47. System.out.println("|");
  48. System.out.println("|");
  49. System.out.println("|_______");
  50.  
  51. String hidden_word;
  52.  
  53. hidden_word= dictionary[index.nextInt(dictionary.length)].toLowerCase();
  54. for (int line=0; line < hidden_word.length(); line++)
  55. {
  56. System.out.print("_ ");
  57. }
  58.  
  59. Scanner keyboard= new Scanner(System.in);
  60. userAnswer= keyboard.nextLine().toLowerCase().charAt(0);
  61.  
  62.  
  63. }
  64.  
  65. public static char[] CharacterCheck(char userAnswer, String[] dictionary, String hidden_word)
  66. {
  67. char[] correctLetters;
  68. //int mistakeCounter;
  69. String answer_check;
  70. answer_check= correctLetters.charAt();
  71.  
  72. for(int i= 0; i < hidden_word.length; i++)
  73. {
  74. if(hidden_word.charAt(i)== userAnswer)
  75. {
  76. correctLetters[i]=userAnswer;
  77. }
  78.  
  79. }
  80.  
  81. return correctLetters;
  82. }
  83. }

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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,190
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 483
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Having Some Trouble with Methods

 
0
  #2
Apr 15th, 2007
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...
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 32
Reputation: DaMoogle is an unknown quantity at this point 
Solved Threads: 0
DaMoogle DaMoogle is offline Offline
Light Poster

Re: Having Some Trouble with Methods

 
0
  #3
Apr 15th, 2007
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC