944,164 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1279
  • Java RSS
Apr 15th, 2007
0

Having Some Trouble with Methods

Expand Post »
Java Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 18
Solved Threads: 0
Light Poster
DaMoogle is offline Offline
34 posts
since Jun 2005
Apr 15th, 2007
0

Re: Having Some Trouble with Methods

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...
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,659 posts
since Dec 2004
Apr 15th, 2007
0

Re: Having Some Trouble with Methods

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.
Reputation Points: 18
Solved Threads: 0
Light Poster
DaMoogle is offline Offline
34 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Homework question
Next Thread in Java Forum Timeline: jdbc: iden authentication using fedora core6





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC