Hangman masking string, unhiding character
<p>Please guys bare with me, i posted a lot of questions and people keep voting to close the question so I hope this one is different. I'm busy writing an hangman application and sofar I'm at the point where I have to write the code to Hide each char in the string (the word being guessed) with "-"... And I've posted a lot of questions on hoe to do it and one of my replies was:<a href="http://stackoverflow.com/questions/12073860/hangman-hide-string-and-then-unhide-each-char-if-guessed-correct">Hangman - hide String and then unhide each char if guessed correct</a></p>

<pre><code>public class HangmanWord {

private static final char HIDECHAR = '_';

private String original;     
private String hidden;  

public HangmanWord(String original) {
    this.original = original;
    this.hidden = this.createHidden();
}

private String createHidden() {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i &lt; this.original.length; i++) {
        sb.append(HIDECHAR);
    }
    return sb.toString();
}

public boolean check(char input) {
    boolean found = false;
    for (int i = 0; i &lt; this.original.length; i++) {
        if (this.original[i].equals(input)) {
            found = true;
            this.hidden[i] = this.original[i];
        }
    }
    return found;
}

//getter and setter
}
</code></pre>

<hr>

<pre><code>public class TestClass() {

public static void main(String[] args) {
    String secret = "stackoverflow";
    int wrongGuesses = 0;
    HangmanWord hngm = new HangmanWord(secret);
    System.out.println(hngm.getHidden()); // _____________
    if (hngm.check('a')) {
        System.out.println(hngm.getHidden()); // __a_________
    }
    else {
       wrongGuesses++;
    }
    //... and so on...
}
}
</code></pre>

<p>I tried to use this code with mine and I had a lot of errors and conclusion it didn't work with my code. In my code i've created an array buttons where if the person clicks on a button I get an message saying if the letter is in the word or not... Now I want to replace this code with where it doesn't give me an message but unhides the char and if there's no char it has to change the image</p>

<p>If possible can anyone explain why it didn't work with my code or can anyone please explain to me what to do...</p>

<p>my button array:</p>

<pre><code>public JButton getButton(final String text){
   final JButton button = new JButton(text);
    button.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
             if(original.toUpperCase().indexOf(button.getText())!=-1){
                 JOptionPane.showMessageDialog(null, "Your word does contain " + text       );
             }
             else{
                 JOptionPane.showMessageDialog(null, "There is no " + text );
                 error++;
                    if(error &gt;= 0) imageName = "hangman1.jpg";
                    if(error &gt;= 1) imageName = "hangman2.jpg";
                    if(error &gt;= 2) imageName = "hangman3.jpg";
                    if(error &gt;= 3) imageName = "hangman4.jpg";
                    if(error &gt;= 4) imageName = "hangman5.jpg";
                    if(error &gt;= 5) imageName = "hangman6.jpg";
                    if(error &gt;= 7) imageName = "hangman7.jpg"; 
             }
             }
             });
             return button;
}
</code></pre>

<p>my full code:</p>

<pre><code>import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
import javax.swing.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Random;
import java.util.List;

public final class Hangman extends JFrame implements ActionListener{
String original = readWord();
int error;
String imageName;

JButton btnAddWord = new JButton("Add New Word");
JButton btnRestart = new JButton("Restart");
JButton btnHelp = new JButton("Help");
JButton btnExit = new JButton("Exit");

JLabel word = new JLabel(original);

static JPanel panel1 = new JPanel();
static JPanel panel2 = new JPanel();
static JPanel panel3 = new JPanel();
static JPanel panel4 = new JPanel();

public Hangman(){
    Container content =getContentPane();
    content.setLayout(new GridLayout(0,1));

   btnAddWord.addActionListener(this);
   btnRestart.addActionListener(this);
   btnHelp.addActionListener(this);
   btnExit.addActionListener(this);

   ImageIcon icon = null;
   if(imageName != null){
   icon = new ImageIcon(imageName);
                    }
  JLabel image = new JLabel();
  image.setIcon(icon);
  panel2.add(image);

   panel3.add(word);
   panel4.add(btnAddWord);
   panel4.add(btnRestart);
   panel4.add(btnHelp);
   panel4.add(btnExit);

    for(char i = 'A'; i &lt;= 'Z'; i++){
        String buttonText = new Character(i).toString();
        JButton button = getButton(buttonText);
        panel1.add(button);
    }
}

public JButton getButton(final String text){
   final JButton button = new JButton(text);
    button.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
             if(original.toUpperCase().indexOf(button.getText())!=-1){
                 JOptionPane.showMessageDialog(null, "Your word does contain " + text     );
             }
             else{
                 JOptionPane.showMessageDialog(null, "There is no " + text );
                 error++;
                    if(error &gt;= 0) imageName = "hangman1.jpg";
                    if(error &gt;= 1) imageName = "hangman2.jpg";
                    if(error &gt;= 2) imageName = "hangman3.jpg";
                    if(error &gt;= 3) imageName = "hangman4.jpg";
                    if(error &gt;= 4) imageName = "hangman5.jpg";
                    if(error &gt;= 5) imageName = "hangman6.jpg";
                    if(error &gt;= 7) imageName = "hangman7.jpg"; 
             }
             }
             });
             return button;
}
public String readWord(){
    try{
        BufferedReader reader = new BufferedReader(new FileReader("Words.txt"));
        String line = reader.readLine();
        List&lt;String&gt; words = new ArrayList&lt;String&gt;();
        while(line != null){
            String[] wordsLine = line.split(" ");
            boolean addAll = words.addAll(Arrays.asList(wordsLine));
            line = reader.readLine();
        }
        Random rand = new Random(System.currentTimeMillis());
        String randomWord = words.get(rand.nextInt(words.size()));
        return randomWord;

}catch (Exception e){
    return null;
}
}
public void actionPerformed(ActionEvent e){
    if(e.getSource() == btnAddWord){
        try{
            FileWriter fw = new FileWriter("Words.txt", true);
            PrintWriter pw = new PrintWriter(fw, true);

            String word = JOptionPane.showInputDialog("Please enter a word: ");

            pw.println(word);
            pw.close();
        }
        catch(IOException ie){
            System.out.println("Error Thrown" + ie.getMessage());
        }
    }
    if(e.getSource() == btnRestart){

    }
    if(e.getSource() == btnHelp){
        String message = "The word to guess is represented by a row of dashes, giving the number of letters and category of the word."
               + "\nIf the guessing player suggests a letter which occurs in the word, the other player writes it in all its correct positions."
               + "\nIf the suggested letter does not occur in the word, the other player draws one element of the hangman diagram as a tally mark."
               + "\n"
               + "\nThe game is over when:"
               + "\nThe guessing player completes the word, or guesses the whole word correctly"
               + "\nThe other player completes the diagram";
       JOptionPane.showMessageDialog(null,message, "Help",JOptionPane.INFORMATION_MESSAGE);
    }
    if(e.getSource() == btnExit){
        System.exit(0);
    }
}

public static void main (String [] args){
    Hangman frame = new Hangman();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 600);
    frame.add(panel1, BorderLayout.NORTH);
    frame.add(panel2, BorderLayout.CENTER);
    frame.add(panel3, BorderLayout.SOUTH);
    frame.add(panel4, BorderLayout.SOUTH);
}
}
</code></pre>
