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
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...
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 =...
This is the java code that I have, but i cannot get the output that I...
This is the java code that I have, but i cannot get the output that I want out of it. i want my output to print the full String Form i stead of just the first letter, and and also print what character is at the specific index instead of leaving it empty. and at the end for Replaced String i want to print both string form one and two with the replaced letters instead if just printing the first...
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length();...
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length(); for (int i = 0; i < array.length; i++) { if (array[i].length() < minLength) minLength = array[i].length(); } return minLength; } public static void main(String[] args) { Scanner in = new Scanner(System.in); String[] strings = new String[50]; for (int i = 0; i < strings.length; i++) { System.out.print("Enter string " + (i + 1) + ": "); strings[i] = in.nextLine(); } System.out.println("Length of smallest...
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[]) ).
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 =...
Here is my java code, I keep getting this error and I do not know how...
Here is my java code, I keep getting this error and I do not know how to fix it: PigLatin.java:3: error: class Main is public, should be declared in a file named Main.java public class Main { ^ import java.io.*; public class Main { private static BufferedReader buf = new BufferedReader( new InputStreamReader(System.in)); public static void main(String[] args) throws IOException { String english = getString(); String translated = translate(english); System.out.println(translated); } private static String translate(String s) { String latin =...
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)...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT