Question

THIS IS FOR JAVA I have to write a method for a game of Hangman. The...

THIS IS FOR JAVA

I have to write a method for a game of Hangman. The word the user is trying to guess is made up of hashtags like so " ###### "
If the user guesses a letter correctly then that letter is revealed on the hashtags like so "##e##e##"

If the user guesses incorrectly then it increments an int variable named count " ++count; "

String guessWord(String guess,String word, String pound) In this method, you compare the character(letter) read from the user to the letters in the original word, then if the letter is present in the original word, then place theletter/lettersin thepound. Return the updated poundstring.

Here is my code so far to help

import java.util.Arrays;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;


public class Project_4 {

   public static void main(String[] args) throws FileNotFoundException
   {
           // have user enter name for file
      
      
       String nameOfFile = "dictionary.txt";
      
      
           String[] wordBank = readFile(nameOfFile);
      
       String thisWord = chooseRandomWord(wordBank);
      
       System.out.println(thisWord);
      
       createPound(thisWord);
       System.out.println(createPound(thisWord));

   }

   public static String guessWord(char guess, String word, String dashes)
   {

       // construct a scanner to read user guesses
      
       Scanner keyboard = new Scanner(System.in);
      
       // create char variable to hold user guess
      
       char userGuess = keyboard.nextLine();
      

   }

   public static String[] readFile(String filename) throws FileNotFoundException
   {
      
      
      
       //read the file
  
       Scanner file = new Scanner(new File(filename));
      
       // read number of lines inside file
      
       int numString = 0;
       while (file.hasNextLine())
       {
           ++numString;
           file.nextLine();
       }
       file.close();
      
       // now we read the file into a string array
   Scanner keyboard = new Scanner(new File(filename));
   String[] words = new String[numString];
  
   // using a for loop to read in each line to the array
  
   for (int index = 0; index < numString; ++index) {
       words[index] = keyboard.nextLine();
   }
   keyboard.close();
   return words;
   }
      
  
  

   public static String createPound(String chosenWord)
   {

           String newWord;
           char[] newWordChar = chosenWord.toCharArray();
          
           for(int idx = 0; idx < chosenWord.length(); ++idx) {
               newWordChar[idx] = '#';
           }
       newWord = String.valueOf(newWordChar);
      
       return newWord;
  
   }

   public static String chooseRandomWord(String[] words)
   {
       int numString = 0;
           for (numString = 0; numString < words.length; ++numString);
                  
               int random = (int) (Math.random() * numString);
              
               String chosenWord = words[random];
               return chosenWord;
           }
      

  

   public static void hangmanImage(int count, String word)
   {
       if (count == 1) {
           System.out.println("Wrong guess, try again");
           System.out.println();
           System.out.println();
           System.out.println();
           System.out.println();
           System.out.println("___|___");
           System.out.println();
       }
       if (count == 2)
       {
           System.out.println("Wrong guess, try again");
           System.out.println(" |");
           System.out.println(" |");
           System.out.println(" |");
           System.out.println(" |");
           System.out.println(" |");
           System.out.println(" |");
           System.out.println(" |");
           System.out.println("___|___");
       }
       if (count == 3)
       {
           System.out.println("Wrong guess, try again");
           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("___|___");
       }
       if (count == 4)
       {
           System.out.println("Wrong guess, try again");
           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("___|___");
       }
       if (count == 5)
       {
           System.out.println("Wrong guess, try again");
           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("___|___");
       }
       if (count == 6)
       {
           System.out.println("Wrong guess, try again");
           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("___|___ / \\");
       }
       if (count == 7)
       {
           System.out.println("GAME OVER!");
           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("___|___ / \\");
           System.out.println("GAME OVER! The word was " + word);
       }
   }

}

Homework Answers

Answer #2

Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate the question. Thank You.

Project_4.java

package c8;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class Project_4 {
   static int count=0;
   public static void main(String[] args) throws FileNotFoundException
   {
       // have user enter name for file
       String nameOfFile = "dictionary.txt";
       String[] wordBank = readFile(nameOfFile);
       String thisWord = chooseRandomWord(wordBank);
       System.out.println(thisWord);
       String poundString=createPound(thisWord);
       Scanner keyboard = new Scanner(System.in);
       // create char variable to hold user guess
  
       while(true){
           System.out.println("The word so far is >>>> "+poundString);
           System.out.println("Enter your guess > ");
           char userGuess = keyboard.nextLine().charAt(0);
           poundString =guessWord(userGuess,thisWord,poundString);
           if(poundString.equalsIgnoreCase(thisWord)){
               System.out.println("Congratulations!!! You found the word!!!!");
               break;
           }
           if(count>=7){
               break;
           }
       }
   }

   public static String guessWord(char guess, String word, String dashes){
       String newString="";
       boolean found = false;
       for(int i=0;i<word.length();i++){
           if(word.charAt(i)==guess){
               newString += guess;
               found = true;
           }else{
               newString += dashes.charAt(i);
           }
       }
       if(!found){
           count++;
           hangmanImage(count, word);
       }
       return newString;
   }
   public static String[] readFile(String filename) throws FileNotFoundException
   {

       //read the file

       Scanner file = new Scanner(new File(filename));

       // read number of lines inside file

       int numString = 0;
       while (file.hasNextLine())
       {
           ++numString;
           file.nextLine();
       }
       file.close();

       // now we read the file into a string array
       Scanner keyboard = new Scanner(new File(filename));
       String[] words = new String[numString];

       // using a for loop to read in each line to the array

       for (int index = 0; index < numString; ++index) {
           words[index] = keyboard.nextLine();
       }
       keyboard.close();
       return words;
   }


   public static String createPound(String chosenWord)
   {

       String newWord;
       char[] newWordChar = chosenWord.toCharArray();

       for(int idx = 0; idx < chosenWord.length(); ++idx) {
           newWordChar[idx] = '#';
       }
       newWord = String.valueOf(newWordChar);

       return newWord;

   }

   public static String chooseRandomWord(String[] words)
   {
       int numString = 0;
       for (numString = 0; numString < words.length; ++numString);

       int random = (int) (Math.random() * numString);

       String chosenWord = words[random];
       return chosenWord;
   }


   public static void hangmanImage(int count, String word)
   {
       if (count == 1) {
           System.out.println("Wrong guess, try again");
           System.out.println();
           System.out.println();
           System.out.println();
           System.out.println();
           System.out.println("___|___");
           System.out.println();
       }
       if (count == 2)
       {
           System.out.println("Wrong guess, try again");
           System.out.println(" |");
           System.out.println(" |");
           System.out.println(" |");
           System.out.println(" |");
           System.out.println(" |");
           System.out.println(" |");
           System.out.println(" |");
           System.out.println("___|___");
       }
       if (count == 3)
       {
           System.out.println("Wrong guess, try again");
           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("___|___");
       }
       if (count == 4)
       {
           System.out.println("Wrong guess, try again");
           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("___|___");
       }
       if (count == 5)
       {
           System.out.println("Wrong guess, try again");
           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("___|___");
       }
       if (count == 6)
       {
           System.out.println("Wrong guess, try again");
           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("___|___ / \\");
       }
       if (count == 7)
       {
           System.out.println("GAME OVER!");
           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("___|___ / \\");
           System.out.println("GAME OVER! The word was " + word);
       }
   }

}

where my text file is

dictionary.txt

first
second
third
fourth
fifth

output

Negative case.

answered by: anonymous
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
I wrote the following java code with the eclipse ide, which is supposed to report the...
I wrote the following java code with the eclipse ide, which is supposed to report the number of guesses it took to guess the right number between one and five. Can some repost the corrected code where the variable "guess" is incremented such that the correct number of guesses is displayed? please test run the code, not just look at it in case there are other bugs that exist. import java.util.*; public class GuessingGame {    public static final int...
This is a java assignment on repl.it my code works but I keep failing the tests....
This is a java assignment on repl.it my code works but I keep failing the tests. Can anyone help me Write the body of the fileAverage() method. Have it open the file specified by the parameter, read in all of the floating point numbers in the file and return their average (rounded to 1 decimal place) For the testing system to work, don't change the class name nor the method name. Furthermore, you cannot add "throws IOException" to the fileAverage()...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
* _Example commands for running this file_ * Compilation: javac Assignment1.java * Execution: java Assignment1 <...
* _Example commands for running this file_ * Compilation: javac Assignment1.java * Execution: java Assignment1 < input.txt * * Reads in a text file and for each line verifies whether the word has * unique characters. * * % cat input.txt * Hello * World * * % java Assignment1 < input.txt * False * True * ******************************************************************************/ import java.util.*; public class SortInput { /* a function for checking uniqueness of characters in a word */ private static boolean isUniqueChar(String...
I am a beginner when it comes to java codeing. Is there anyway this code can...
I am a beginner when it comes to java codeing. Is there anyway this code can be simplified for someone who isn't as advanced in coding? public class Stock { //fields private String name; private String symbol; private double price; //3 args constructor public Stock(String name, String symbol, double price) { this.name = name; this.symbol = symbol; setPrice(price); } //all getters and setters /** * * @return stock name */ public String getName() { return name; } /** * set...
Hello, I am trying to create a Java program that reads a .txt file and outputs...
Hello, I am trying to create a Java program that reads a .txt file and outputs how many times the word "and" is used. I attempted modifying a code I had previously used for counting the total number of tokens, but now it is saying there is an input mismatch. Please help! My code is below: import java.util.*; import java.io.*; public class Hamlet2 { public static void main(String[] args) throws FileNotFoundException { File file = new File("hamlet.txt"); Scanner fileRead =...
//please debug program // need Scanner output import java.util.Scanner; // Display every character between Unicode 65...
//please debug program // need Scanner output import java.util.Scanner; // Display every character between Unicode 65 and 122 // Start new line after 20 characters public class DebugSix2 { public static void main(String args[]) { Scanner sc = new Scanner(System.in); char letter; int a; final int MIN = 65; final int MAX = 122; final int NUMPERLINE = 200; final int STOPLINE1 = 0; final int STOPLINE2 = STOPLINE1 + NUMPERLINE; for(a = MIN; a <= MAX; a++) { letter...
Refactor the following program to use ArrayList instead of Arrays. You can google "Java ArrayList" or...
Refactor the following program to use ArrayList instead of Arrays. You can google "Java ArrayList" or start with the link below: https://www.thoughtco.com/using-the-arraylist-2034204 import java.util.Scanner; public class DaysOfWeeks { public static void main(String[] args) { String DAY_OF_WEEKS[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; char ch; int n; Scanner scanner = new Scanner(System.in); do { System.out.print("Enter the day of the Week: "); n = scanner.nextInt() - 1; if (n >= 0 && n <= 6) System.out.println("The day of the week is " + DAY_OF_WEEKS[n] + ".");...
In Java ReWrite the for loop program code example 3, above as a while loop. Code...
In Java ReWrite the for loop program code example 3, above as a while loop. Code Example 3 – Try it import java.util.Scanner; public static void main(String args[]) { int count = 0; int maxNumber = 0; int enteredNumber = -9999999; System.out.println("This program determines which entered number is the largest "); System.out.println("Enter count of numbers to check: "); Scanner scanIn = new Scanner(System.in); double count = scanIn.nextDouble(); scanIn.close(); // Print out message – input number – compare to see if...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT