0

I"m currently working a little self project to create a hangman game. I want it so the computer prints the missing letters you have to find as underscores and the letters you have already found in the place where the hidden word would normally be. I'm stuck on the part where I want the computer to replace the underscores with letters every time the user inputs the correct char.

import java.util.Arrays;
import java.util.Scanner;

public class HangmanGame {

    static String hiddenWord = "marcus";
    static int triesLeft = 6;
    static boolean done = false; 
    static int length = hiddenWord.length();
    static Scanner input = new Scanner(System.in);
    final static int maxLength = 20;
    static char[] letters = new char[20];
    static char[] repeatChecker = new char[20]; //this is to help prevent the user from putting the same char multiple times
    static char[] test = new char[20];
    static char[] fillWord = new char[length];
    static int var;
    static int var2;
    static char underscore = '_';




    public static boolean contains(char[] arr, char i) {
          for (char n : arr) {
             if (i == n) {
                return true;
             }
          }
          return false;
       }
    public static void stringToArray(String s){
        char[] stringArray = s.toCharArray();
        for (char output : stringArray){
            var2++;
            test[var2] = output;
            //System.out.println(test);

        }

    }
    public static void createSpaces(int n){
        for (int i = 0; i <= n-1; i++){
            fillWord[i] = underscore;
        }
        System.out.println(fillWord);
    }


    public static void arrayLetters(){
        System.out.println("Please enter a letter to guess: ");
        char charInput = input.next().charAt(0);
        for (int i = 0; i < length; i++){
            char wordLetter = hiddenWord.charAt(i);
            letters[i] = wordLetter;
            if (contains(letters, charInput)){
                if (contains(repeatChecker, charInput)){
                    System.out.println("You already did that word, try again!");
                    arrayLetters();
                }
                repeatChecker[var] = charInput;
                var++;
                fillWord[i] = fillWord[i].replace(charInput); 
                System.out.println("Nice! There is a " + charInput + " in this word!");
                System.out.println(repeatChecker);
                System.out.println("You have " + triesLeft + " tries left!\n");
                arrayLetters();

            }
            if (i == length-1){
                System.out.println("There is no " + charInput + " in this word!");
                triesLeft--;
                System.out.println("You have " + triesLeft + " tries left!\n");
                if (triesLeft <= 0){
                    System.out.println("You failed!");
                    System.exit(0);

                }
                arrayLetters();
            }
        }


    }

    public static void main(String[] args) {
        System.out.println("Welcome to my hangman game!");
        stringToArray(hiddenWord);
        //System.out.println(length);
        createSpaces(length);
        arrayLetters();
    }




}

How can I use the replace(), or another function, correctly to make it so then my code can replace the underscores with a character every time the user inputs a correct char that the hiddenWord contains.

5
  • 3
    fillWord[i] = charInput? If fillWord array is strings generic type then you can do: fillWord[i] = "" + charInput. Commented Aug 5, 2016 at 23:43
  • You've left out some important parts of your code. But it does look like you're going for a simple assignment. Commented Aug 5, 2016 at 23:44
  • What is fillWord? An array of char or String or StringBuilder what? I don't see any replace(char) method on those objects. Commented Aug 5, 2016 at 23:52
  • My bad, I should've just pasted all of the code to make it simpler. By fillWord, it's the array I created to store the blank spaces for the characters to be inserted in. Commented Aug 5, 2016 at 23:55
  • If you plan on printing out the char array as a string, a StringBuffer or StringBuilder class may be more preferred Commented Aug 5, 2016 at 23:59

2 Answers 2

1

You have to write

fillWord[i] = charInput;

And two lines later

System.out.println(fillWord);

You printed your repeatChecker, not the fillWord.

Sign up to request clarification or add additional context in comments.

3 Comments

Yeah the problem was me printing repeatChecker. I got it working, the help is very appreciated!
@Costi Ciudatu Sure you can println a char[], since System.out.println is overloaded specifically for char[]. The crappy println is for other Arrays.`
@garnulf you're right! I'll remove my malicious and totally wrong comment.
0

You can directly do:

fillWord[i] = charInput;

Arrays don't require replace methods anyway.

2 Comments

Okay, I tried doing your suggestion, however it doesn't place the char in the correct area. For example, in this situation to make things simple the hiddenWord is "marcus", fillWord would be like this: "_ _ _ _ _ _". However when I enter any char that is in "marcus" it takes place in the beginning of the array. If I would enter "a", the console would print just "a", without the underscores as well.
Print out fillWord to see the changes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.