-1

I'm still learning Java and me and my classmate are working on this assignment code game hangman. We have a problem in the part printing the original word, because we want the word to be hidden, but we need the word to be printed, not hidden on the final output. What can we revise in our code to make it print properly??

import java.util.Scanner;
public class GameHangman {

  public static void main(String[] args) {
    System.out.println("Welcome! To the HANGMAN game.");
    System.out.println("Guess the word by guessing each letter.");
    System.out.println("LET'S START!");
    String[] words = {"superman","batman","spiderman"};
    int t = words.length;
    int x, y, miss;
    String w;
    char l, c='y';
    
    Scanner input = new Scanner(System.in);
    while (c=='y')
    {
        x = (int)(Math.random()*(t));
        w = words[x];
        char[]chars = w.toCharArray();
        var hidden = new char[w.length()];
        for(int i = 0; i < hidden.length; i++)
        {
            hidden[i] = '*';
        }
        boolean z = false;
        int count = 0;
        miss = 0;
        while(!z)
        {
            System.out.print("\n(guess) Enter a letter ("+new String(hidden)+"): ");
            l = input.next().charAt(0);
            y = 0;
            for(int i = 0; i < hidden.length; i++)
            {
                if(chars[i]==l)
                {
                    y++;
                    if(hidden[i]=='*')
                    {
                        hidden[i]=l;
                        count++;
                        y++;
                    }
                }
            }
            if(y==1)System.out.println("\nOOPS!("+l+") already present, try other letters.");
            else if (y==0)
            {
                miss++;
                System.out.println("\nYIKES!("+l+") is not in the word, guess again.");
            }
            if(count==hidden.length) z = true;
        }
        System.out.println("\nGreat!! You guessed the word("+hidden+")!!!");
        System.out.println("You were wrong "+miss+" tries.");
        System.out.print("\nDo you want to continue with another game word? "
                + "\nEnter yes or no: ");
        c = input.next().charAt(0);
    }


  }

}

in this part, we print our final word

System.out.println("\nGreat!! You guessed the word("+hidden+")!!!");

and this is what we are getting output. we need to print with not being hidden.

enter image description here

Jared
  • 2,999
  • 1
  • 28
  • 39
Cy.Car
  • 3
  • 1
  • what happens if you change to `System.out.println("\nGreat!! You guessed the word("+new String(hidden)+")!!!");` like how you use it previously? – depperm Sep 15 '21 at 12:18
  • 1
    Simply print `w` instead of `hidden`. And if you want to print a `char[]` as a string, use `new String(hidden)` to turn the `char[]` into a `String`. – Joachim Sauer Sep 15 '21 at 12:18
  • `hidden` is an array. so when you print it out, it prints the array object, not its content. See https://www.javatpoint.com/how-to-convert-char-array-to-string-in-java – Bentaye Sep 15 '21 at 12:18
  • So print `words[x]` instead of `hidden`? Or am I misunderstanding something? This is probably also a good time for you to start familiarizing yourself with the use of [a debugger](https://stackoverflow.com/q/25385173/328193) to observe the specific runtime values and behavior of your code, which will help you better understand what each variable is. Using *meaningful variable names* will also help you with that. – David Sep 15 '21 at 12:20
  • And in the future [please do not upload images of code/errors when asking a question.](//meta.stackoverflow.com/q/285551) – Joachim Sauer Sep 15 '21 at 12:22
  • OH!! okay noted, so you can just print it without using the +hidden+ I thought you really need to use it, and because I thought I already converted my w that's why I cant use it to print. THANK YOU SO MUCH! – Cy.Car Sep 15 '21 at 12:35
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 22 '21 at 06:56

1 Answers1

0

Here are two methods to convert your char array hidden into a String for printing out.

      // First option: Create a String object
      String str1 = new String(hidden);
      System.out.println("\nGreat!! You guessed the word("+str1+")!!!");
 
      // Second option: Using valueOf method
      String str2 = String.valueOf(hidden);
      System.out.println("\nGreat!! You guessed the word("+str2+")!!!");

Alternatively, you can also print your String variable w and avoid calling these methods.

Oz_
  • 43
  • 5