simpen
simpen

Reputation: 134

Replace multiple characters in a String at different positions

I'm currently making a "Hangman" type game in Java but I've stumbled upon a problem.

static String word = JOptionPane.showInputDialog(null, "Enter word");
static String star = "";
public static Integer word_length = word.length();
public static Integer nrOfAttempts = 12;
public static Integer remainingAttempt = nrOfAttempts;

public static void main(String[] args) {
    // String görs med lika många stjärnor som ordet har tecken
    for (int i = 0; i < word.length(); i++) {
        star += "_";
    }
    attempt();
}

public static void attempt(){

    for(int o = 0; o < nrOfAttempts; o++) {
        String attempt = JOptionPane.showInputDialog(null, "Enter first attempt");

        if (attempt.length() >  1) {
            JOptionPane.showMessageDialog(null, "Length of attempt can only be one character long");
            attempt();

        } else {

            if (word.contains(attempt)) {
                int attemptIndex = word.indexOf(attempt);
                star = star.substring(0,attemptIndex) + attempt + star.substring(attemptIndex+1);


                JOptionPane.showMessageDialog(null, "Hit!\nThe word is now:\n"+star);
                nrOfAttempts++;

                if (star.equals(word)) {
                    JOptionPane.showMessageDialog(null, "You've won!");
                    System.exit(0);
                }
            } else {
                remainingAttempt--;
                JOptionPane.showMessageDialog(null, "Character not present in chosen word\nRemaining Attempts: "+remainingAttempt); 
            }   
        }
    }
    JOptionPane.showMessageDialog(null, "Loser!");
}

When I want to replace the specific characters at specific places in the "star" String (the word consisting of underscores), it only replaces the first character that matches. It does this over and over again, so that it is impossible to win.

Words such as "potato" and "cool" do, therefore, not work.

What I want it to do is to replace ALL of the matching letters, not just the first one it sees. Is it possible to do this without creating an array?

Upvotes: 1

Views: 1285

Answers (2)

thibsc
thibsc

Reputation: 4049

To replace all matching letters step by step, you can use regex replaceAll(String regex, String replacement): String doc
Example:

String word = "potato";
String start = "______";
String attempt = "o";

start = word.replaceAll("[^"+attempt+"]", "_");
// start = "_o___o";

attempt += "t";
start = word.replaceAll("[^"+attempt+"]", "_");
// start = "_ot_to";

attempt += "p";
start = word.replaceAll("[^"+attempt+"]", "_");
// start = "pot_to";

attempt += "a";
start = word.replaceAll("[^"+attempt+"]", "_");
// start = "potato"; -> win

Upvotes: 0

Manos Nikolaidis
Manos Nikolaidis

Reputation: 22224

Here is how you would do letter replacement in your case for the whole string:

int attemptIndex = word.indexOf(attempt);
while (attemptIndex != -1) {
    star = star.substring(0, attemptIndex) + attempt + star.substring(attemptIndex + 1);
    attemptIndex = word.indexOf(attempt, attemptIndex + 1);
}

In the second version of indexOf an index to start searching from is provided. And that's a +1 to avoid finding the same letter again. Documentation for indexOf.

Note that using a char array of StringBuilder may be a more efficient solution as it would avoid creating many temporary strings.

Upvotes: 3

Related Questions