944,161 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 547
  • Java RSS
Nov 4th, 2009
0

Java hang man, help please.

Expand Post »
Hi, I'm new here. I have been looking around for the last few months. Very new to java and I need help with this assignment. I am to program hangman, I know that this has been asked multiple times. I just cannot seem to find the answers that I need. This is what I have so far. I can't seem to figure out what I need to do to print the letters in the correct underscore. I was thinking the block comment section can be used to search for used letters. I think that is all that I have to do in my assignment, and it's the stuff that I don't understand or cannot think of a way to do it. Any help would be greatly appreciated.
Thanks in advance
JAVA Syntax (Toggle Plain Text)
  1.  
  2. import java.util.*;
  3. import java.io.*;
  4.  
  5. public class Assig3
  6. {
  7.  
  8.  
  9. public static void main(String [] args) throws IOException
  10. {
  11. boolean winYes;
  12. int wins;
  13. int i;
  14. int guesses = 7;
  15. char [] letters = new char [26];
  16. int index;
  17. char lettersGuessed;
  18. int Wordquit = 2;
  19. int missWord = 0;
  20.  
  21.  
  22. Scanner inScan = new Scanner(System.in);
  23. System.out.println("Please enter your name\n");
  24. Name.getName(inScan.next());
  25. //Loads the file as specifed by user
  26. wordServer theWords = new wordServer();
  27. System.out.println("Enter file name: ");
  28. Scanner keyScan = new Scanner(System.in);
  29. String fName = keyScan.next();
  30. File inFile = new File(fName);
  31. Scanner fScan = new Scanner(inFile);
  32. theWords.loadWords(fScan);
  33.  
  34. // creates current word object
  35. String current;
  36. current = theWords.getNextWord();
  37. StringBuilder guessedWord = new StringBuilder();
  38.  
  39. //Shows user underscores of letters needed.
  40. do
  41. {
  42. System.out.print("Your word is: ");
  43. i = 0;
  44.  
  45. while( i < current.length())
  46. {
  47. i++;
  48. guessedWord.append('_');
  49. }
  50.  
  51. System.out.println(guessedWord);
  52. index = 0;
  53. while(guesses > 0)
  54. {
  55. System.out.println("You have " + guesses + " left."); // tells user number of guesses left
  56. }
  57. for(i = 0; i <= index - 1; i++)
  58. {
  59. System.out.println(letters[i]);
  60. }
  61.  
  62. //prompts user to make a guess.
  63. System.out.println("Please make a guess: ");
  64. lettersGuessed = inScan.nextChar();
  65. /*
  66. char guess = letter.charAt(0);
  67. boolean correctGuess = false;
  68. for(int k = 0; k < current.length(); k++)
  69. {
  70. if(guess == digit[i])
  71. {
  72. hidden[i] = guess;
  73. correctGuess = true;
  74. }
  75. }
  76. */
  77.  
  78. letters[index] = lettersGuessed;
  79. index++;
  80.  
  81.  
  82. System.out.println("Enter 0 to quit word: ");
  83. System.out.println("Enter 1 to quit word: ");
  84. wordQuit = inScan.nextInt();
  85. while(wordQuit = 2);
  86.  
  87.  
  88. }
  89. }

JAVA Syntax (Toggle Plain Text)
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class wordServer
  5. {
  6. String [] words;
  7. int num;
  8. int [] usedInd;
  9. int i;
  10. String empty = " ";
  11.  
  12. public wordServer()
  13. {
  14. i = 0;
  15. }
  16.  
  17. public void loadWords(Scanner S) throws IOException // loads "dictionary" of words.
  18. {
  19. num = S.nextInt();
  20. words = new String [num];
  21. usedInd = new int [num];
  22. for (int i = 0; i < num; i++)
  23. words[i] = S.nextLine();
  24. S.close();
  25. }
  26.  
  27. public String getNextWord() // Finds a random index number. then returns a random word.
  28. {
  29.  
  30. do
  31. {
  32. int ranInd = (int)(words.length * Math.Random()) + 1
  33. int hit = 0;
  34. for(int j = 0; j <= i - 1; j++)
  35. {
  36. if(ranInd == usedInd[j])
  37. {
  38. hit = 1;
  39. }
  40. }
  41. while(hit = 1);
  42.  
  43. usedInd[i] = ranInd;
  44. i++;
  45. return words[ranInd]
  46. }
  47. }
  48. }

I also have one last class which is hangPlayer. All this has is holding information of the player. name, wins, misses. And then a StringBuilder which returns the information in a neat and organized way.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
skelator is offline Offline
4 posts
since Nov 2009
Nov 4th, 2009
0
Re: Java hang man, help please.
Let me get this straight before I dive into trying to help you. So you are displaying the String as "_ _ _ _" initially, where every second character is a space? Then once the user guesses a correct letter, you replace the String with it? (So like "_ _ A _" after they correctly guess A)? If you aren't doing it like that then how are you saving the correct String? And how are you saving the String that you display to the user?
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Nov 4th, 2009
0
Re: Java hang man, help please.
I display the string as underscores. Almost exactly as you have. Instead of it being "_ _ _ _" there would be no space between letters "____". When A was guessed it would show "__A_". <----- this is what I do not understand how to do.
Also what do you mean by saving the string that you display to the user. As in what is holding the underscores?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
skelator is offline Offline
4 posts
since Nov 2009
Nov 4th, 2009
0
Re: Java hang man, help please.
To make it easier on yourself, you should have two Strings. One String should contain the correct word. The other String should contain what you are displaying to the user. If you do this, when the user guesses a letter, you can say

Java Syntax (Toggle Plain Text)
  1. if (string.contains("A"){
  2. // In here use the String class's replaceAll(oldChar, newChar) method.
  3. }else{
  4. //guess invalid, do other stuff
  5. }

If you don't know what the replaceAll(oldchar, newChar) method is or how it is used, check http://java.sun.com/j2se/1.5.0/docs/...ng/String.html
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Nov 5th, 2009
0
Re: Java hang man, help please.
I worked on it some more between studying for other classes. It compiles now but with an error. This is the error.
Java Syntax (Toggle Plain Text)
  1. Exception in thread "main" java.util.InputMismatchException
  2. at java.util.Scanner.throwFor(Scanner.java:819)
  3. at java.util.Scanner.next(Scanner.java:1431)
  4. at java.util.Scanner.nextInt(Scanner.java:2040)
  5. at java.util.Scanner.nextInt(Scanner.java:2000)
  6. at Assig3.main(Assig3.java:89)
  7. mt-wireless-pittnet-27-70:assignment3 justin$
This is my code so far.
Java Syntax (Toggle Plain Text)
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class Assig3
  5. {
  6.  
  7.  
  8. public static void main(String [] args) throws IOException
  9. {
  10. boolean winYes;
  11. int wins;
  12. int i;
  13. int guesses = 7;
  14. char [] letters = new char [26];
  15. int index;
  16. String lettersGuessed;
  17. int wordQuit = 2;
  18. int missWord = 0;
  19. int k;
  20. char guessedLetter;
  21.  
  22.  
  23. Scanner inScan = new Scanner(System.in);
  24. //Loads the file as specifed by user
  25. wordServer theWords = new wordServer();
  26. System.out.println("Enter file name: ");
  27. Scanner keyScan = new Scanner(System.in);
  28. String fName = keyScan.next();
  29. File inFile = new File(fName);
  30. Scanner fScan = new Scanner(inFile);
  31. theWords.loadWords(fScan);
  32.  
  33. // creates current word object
  34. String current;
  35. current = theWords.getNextWord();
  36. StringBuilder guessedWord = new StringBuilder();
  37.  
  38. System.out.println("Please enter your name: ");
  39.  
  40. k = 0;
  41. //Shows user underscores of letters needed.
  42. do
  43. {
  44. System.out.print("Your word is: ");
  45. i = 0;
  46.  
  47. while( i < current.length())
  48. {
  49. i++;
  50. guessedWord.append('_');
  51. }
  52.  
  53. System.out.println(guessedWord);
  54. index = 0;
  55.  
  56. System.out.println("You have " + guesses + " left."); // tells user number of guesses left
  57. for(i = 0; i <= index - 1; i++)
  58. {
  59. System.out.println("So far you have guessed: ");
  60. System.out.println(letters[i]);
  61. }
  62.  
  63. //prompts user to make a guess.
  64. System.out.println("Please make a guess: ");
  65. lettersGuessed = inScan.next();
  66. guessedLetter = lettersGuessed.charAt(0);
  67. StringBuilder correctGuess = new StringBuilder();
  68. int hit = 0;
  69. for(int j = 0; j < current.length(); j++)
  70. {
  71. if(guessedLetter == current.charAt(j));
  72. {
  73. hit = 1;
  74. guessedWord.setCharAt(j, guessedLetter);
  75. }
  76. }
  77.  
  78. letters[k] = guessedLetter;
  79. k++;
  80.  
  81. System.out.println("Enter 0 to quit word: ");
  82. System.out.println("Enter 1 to quit word: ");
  83. wordQuit = inScan.nextInt();
  84. }
  85.  
  86. while(wordQuit == 2);
  87.  
  88.  
  89. }
  90. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
skelator is offline Offline
4 posts
since Nov 2009
Nov 5th, 2009
0
Re: Java hang man, help please.
Java Syntax (Toggle Plain Text)
  1. while(wordQuit == 2);

What is that for? ^
And what is your correctGuesses variable for? You never used it.

And the reason you got InputMismatchException is because your scanner tried to read in one type of (String, int, double, etc) but your input did not have that type immediately in it. See here
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Nov 5th, 2009
0
Re: Java hang man, help please.
The reason wordQuit and correctGuesses are there are for the last bit that needs to be done. I plan on using wordQuit for when the users presses 0, or 1. 0 meaning the give up on the word and 1 meaning they quit the program completely. so while wordQuit = 2 the loop will continue. I don't know if I am actually doing that correctly or not. CorrectGuesses well I guess really that doesn't need to be there. It doesn't make much sense. Although I need some way for a correctly guessed word to be counted or missed if it was missed.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
skelator is offline Offline
4 posts
since Nov 2009

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: Reasigning variables
Next Thread in Java Forum Timeline: Java word guessing game - problem





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


Follow us on Twitter


© 2011 DaniWeb® LLC