theProgrammer101 theProgrammer101 - 1 year ago 40
Java Question

How to change the value of a substring?

I'm trying to make a classic hangman program(the GUI is fine, no problem with it), where the user guesses one letter at a time. The problem is, when the user guesses a letter, it replaces the place of " _ " with the letter guessed(if the letter is in the word), but when the next letter is guessed, it forgets about the last guess, and the place goes back to " _ ". Sorry if that's unclear, here's an example

Word is tiger,
user guesses "g",
outputs _ _ g _ _,
user guesses "r",
outputs _ _ _ _ r.


How I want the code to work:

Word is tiger,
user guesses "g",
outputs _ _ g _ _,
user guesses "r",
outputs _ _ g _ r.


How can I achieve this? keep in mind that tiger was just an example word, words are pulled randomly from an array. I think the only way to do this is to change the value of the substring, but I don't know how to do this. As of now, the user guesses the letter from the GUI window(where the man himself is) and submits the answer by pressing a button. The output is just printed on console for now.

Here's the troublesome part of the program:

// guess is the name of JTextPane next to button
// randWord is the word pulled randomly from the array
private void guessLetter() {
guessedLetter = guess.getText();
if (randWord.contains(guessedLetter)){
reveal(); // method below
} else {
wrongGuesses++; // when wrongGuesses adds,
update(); // another body part is added
}
}

private void reveal() {
int start = randWord.indexOf(guessedLetter); //gets the index of the letter guessed
String print = blanks.substring(0, start) + guessedLetter + blanks.substring(start+1); //reveals the letter
System.out.println(print);//prints the revealed letter in correct spot
}

Answer Source

You can simply make a private variable(I named mine currentWord), and set it equal to blanks. Then you need to make some minor modifications to your code:

public class Hangman extends JFrame {
    private String currentWord;
    //when "blanks" gets assigned a value, make "currentWord" equal to it   
    //... 
    //...
private void guessLetter() {
    guessedLetter = guess.getText();
    if (randWord.contains(guessedLetter)){
        reveal();
    } else {
        wrongGuesses++;
        update();
    }
}

private void reveal() {
    int start = randWord.indexOf(guessedLetter);
    currentWord = currentWord.substring(0, start) + guessedLetter + currentWord.substring(start+1);
    System.out.println(currentWord);
    if (currentWord.equals(randWord)){
        System.out.println("You win!!");
    }
}

This will change currentWord one letter at a time, until you run out of guesses, or currentWord equals the original word. This works by changing one substring at a time, assigning currentWord to that value. Then that word is printed, changed with the next guess, printed, etc. until you win or lose.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download