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...
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...
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 =...
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] + ".");...
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...
Write Java program Lab51.java which takes in a string from the user, converts it to an...
Write Java program Lab51.java which takes in a string from the user, converts it to an array of characters (char[] word) and calls the method: public static int countVowels(char[]) which returns the number of vowels in word. (You have to write countVowels(char[]) ).
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import java.io.*; //This imports input and output (io) classes that we use 3 //to read and write to files. The * is the wildcard that will 4 //make all of the io classes available if I need them 5 //It saves me from having to import each io class separately. 6 /** 7 This program reads numbers from a file, calculates the 8 mean (average)...
Task #4 Calculating the Mean Now we need to add lines to allow us to read...
Task #4 Calculating the Mean Now we need to add lines to allow us to read from the input file and calculate the mean. Create a FileReader object passing it the filename. Create a BufferedReader object passing it the FileReader object. Write a priming read to read the first line of the file. Write a loop that continues until you are at the end of the file. The body of the loop will: convert the line into a double value...
is there anything wrong with the solution. the question are from java course Write a main...
is there anything wrong with the solution. the question are from java course Write a main method that will request the user to enter Strings using a JOptionPane input dialog. The method should continue accepting strings until the user types “STOP”.       Then, using a JOptionPane message dialog, tell the user how many of the strings begin and end with a digit. Answer: import javax.swing.*; public class IsAllLetters {     public static void main(String[] args) {         String input;         int count =...