Q:

Java access ArrayList without for loop

I'm trying to make a hangman type game but I'm having difficulty with writing the method that returns and updates the word which the user is trying to guess. So for example if the word to guess was 'hello' I would want it to be printed as * but if someone guesses 'e', it should update to e**. I have kind of got this to work but it loops through two ArrayLists which is not exactly what I want:
public String getVisible() {
    String rtn = null;
    for(Character b: brokenphrases){

        for(Character c:characters){
            if(b == c){
                rtn = rtn + b;

            }
        }

        if(b.toString().equals(" ")){
            rtn = rtn +" ";

        }
        else{

            rtn = rtn + "*";
        }

    }

    return rtn;
}

The idea is that the ArrayList of characters contains the input that the user puts in, and the ArrayList of brokenphrases has the word that needs to be guessed, broken down into single characters so they can compare the two. What happens now is when I run the program I get my word to guess, for example hello printed as *  but as I guess letters the the text becomes doubled up instead of being replaced. So if I was to type in h ,e ,l ,o , the system prints out h*e*l*l*o* instead out getting rid of the asterix. I figure that it's to do with using two for loops but I'm not sure on how to mend it. Any advice would be appreciated. Thanks.

A:

You can simplify your code to get something like this:
public String getVisible() {
    String rtn = null;

    for(Character b: brokenphrases){
        if(characters.contains(b)){
            rtn = rtn + b;
        } else {
            rtn = rtn + "*";
        }
    }

    return rtn;
}

You iterate over all characters of your target word (brokenphrases), for example hello. If the character is in characters, you add the character to the output. If not, you add *.
If you guessed h and l you would get h*ll*.
No need to add blankspace.

