0
import java.io.*;
import java.util.*;

public class Hangman
{
    public static void main(String[] args) throws IOException
    {
        Scanner in;
        in = new Scanner(System.in);
        String secretWord = "dinosaur";
        char[] secretWordArray = new char[] {'d', 'i', 'n', 'o', 's', 'a', 'u', 'r'};
        char[] ansArray = new char[8];
        int lives = 6;
        
        System.out.println("Starting game...\n\n");
        
        System.out.println("Your secret word is: ");
        //Displays the 'fillers': ******* 
        for(int i = 0; i<secretWordArray.length; i++)
        {
            ansArray[i] = '*';
        }
        
        while(lives>0)
        {
            System.out.println("\nInput a letter: ");
            String guessString = in.nextLine();
            char guess = guessString.charAt(0);
            
            if(secretWord.contains(guessString))
            {
                for(int j = 0; j<secretWordArray.length; j++) 
                {
                    if(secretWordArray[j] == guess)
                    {
                        ansArray[j] = guess;
                        System.out.println("The word contains the letter " + guess + "!");
                        System.out.println("Your word looks like this: " + ansArray);
                        
                    }
                } //end for-loop 
            } //end if (guess correct statement)
        
            else
            {
                System.out.println("The word does not contain the letter " + guess + ".\nYou have " + lives + " lives left.");
                System.out.println("Your word looks like this: " + ansArray);
                lives--;
            }
        } //end while (lives loop)
        
    } //end main
} //end class

Coding a simple hangman game for CS101 final project. Stuck on what to do next.

I used a char[] secretWordArray to breakdown the word into each letter. The char[] ansArray is a blank array which at the start of the program, is equal to * * * * * * * . If the user guesses 'o', ansArray is changed to * * * o * * * *

I'm not really sure if there's a better alternative than using a char[] arr to go about this? Or if my thought process is right and I'm just coding it wrong? Help pls

b-rad90
  • 19
  • 4
  • The code you posted does not compile because `System.out.println("Your word looks like this: " + word);` will result in a compilation error because there is no variable called `word` in your code defined anywhere. Because the code doesn't compile I'm not sure how you manage to get any result at all. Edit: You have now edited your question and I'm not even sure what it is your asking. What specific problem is it you are facing? Simply asking "Am I on the right track" is a really broad question. – OH GOD SPIDERS Nov 08 '22 at 16:31
  • Oops. Forgot to edit that. Re-edited it now – b-rad90 Nov 08 '22 at 16:33
  • ansArray[] is outputting a value of '[C@3b6eb2ec'. Why is the letter not being replaced when I set ansArray[j] = guess;? – b-rad90 Nov 08 '22 at 16:36
  • Use `Arrays#toString` before printing the array. – Rogue Nov 08 '22 at 16:38
  • See: [What's the simplest way to print a Java array?](https://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array) – OH GOD SPIDERS Nov 08 '22 at 16:39
  • Array.toString worked. Can't believe it was that simple. Thank you – b-rad90 Nov 08 '22 at 16:40

0 Answers0