0

I have tried all night to find an answer and haven't managed to get anything. What i am trying to do is to generate 2 words, a secretWord and a hiddenWord. The hidden word is the same length as the secretWord but with all the characters replaced with a symbol. I have seen how to do this with setBuilder or arrays, but im not at the point where i am comfortable using them, and would like to know if it is possible without them.

Im sorry if this seems rambling, but no where i have looked has a sollution that doesnt include either arrays or setbuilder

public class Hangman {




   public static void main( String[] args ) {

      Hangman game = new Hangman();
      game.initialize("Happiness");
      System.out.println( "Lets play a round of hangman");
      game.playGame();

      Scanner keyboard = new Scanner(System.in); 
      char guess = 'a';
      int secretLength;

      public class playGame {{
         while (isFound == false)
            System.out.println("The disguised word is " + game.getDisguisedWord);
            System.out.println("Guess a letter: ");
            game.makeGuess();
   }}

   private class isFound {{
         if (getSecretword.secretWord.equals(getDisguisedWord.disguisedWord)){
         }
      }}
      public class getDisguisedWord {{
      return disguisedWord;
      }}



      public class getSecretWord {
         initialize.getsecretWord();

      }

      private class makeGuess {{
         char guess = keyboard.next().charAt( 0 );
         for (int index = 0; index < secretWord.length(); index++)
            if (initialize.secretWord.charAt(index) == guess)
               hiddenWord.setCharAt(index, guess);
      }}

      public class initialize {{
         this.secretWord();
         int secretLength = secretWord.length(); 
         while (secretLength > 0){
            hiddenWord += '?';
         }
      }


   }
J Welch
  • 25
  • 8
  • 5
    Your tags pretty much spell out the answer. `String.replace` is your buddy here. Try it. Also, share what you have tried so far so people can help you and see where you're stuck. – Ori Lentz Mar 28 '16 at 06:08
  • Where are your codes ? – Suresh Atta Mar 28 '16 at 06:08
  • how about http://stackoverflow.com/a/2807731/4297364 ? – Moishe Lipsker Mar 28 '16 at 06:12
  • mycode is really bad for this.... but here goes nothing. i dont know how i would be able to use string.rplace as each character is different. is there a way to select them all for replacement? – J Welch Mar 28 '16 at 06:14
  • You can use `replaceAll(String regex, String replacement)` method from `String` class and replace all characters of your string. – Yaroslav Rudykh Mar 28 '16 at 06:46

1 Answers1

0

You can do it using String.replaceAll():

hiddenWord = secretWord.replaceAll(".", yourSymbol);

The "." in regular expressions means any character.

Gregory Prescott
  • 574
  • 3
  • 10