Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying to make a hangman program, can't get it to show guessed letters
    text
    copied!<p>I'm trying to create a hangman program. The phrase they have to guess is "bad hair day", and they see "<strong>* *<em></strong> *</em>*". When the user inputs a character nothing changes. I'm not 100% sure were I am going wrong but maybe it's in the passwordlabel2 or somewhere in the loop.</p> <p>Demo Class</p> <pre><code> public class SecretPhrase { int wrong = 0; //ignore for now String phrase = "Bad hair day"; //hidden, what the user has to guess String hiddenPhrase = "*** **** ***"; //what the user originally sees public void changeLetter(char input) { StringBuilder checker = new StringBuilder(input); StringBuilder(hiddenPhrase); boolean wrongGuess = true; for (int i=0; i&lt;phrase.length(); i++) { if (phrase.charAt(i) == input){ checker.setCharAt(i, input); wrongGuess = false; } } hiddenPhrase = checker.toString(); if (wrongGuess){ wrong++; } } private void StringBuilder(String hiddenPhrase) { // TODO Auto-generated method stub } } </code></pre> <hr> <p>UI Class</p> <pre><code>import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JApplet; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JTextField; public class SecretPhraseUI extends JApplet implements ActionListener { SecretPhrase phrase = new SecretPhrase(); JLabel passwordLabel = new JLabel("Enter a letter to guess the phrase." ); //sets label to display message JLabel passwordLabel2 = new JLabel( phrase.hiddenPhrase ); //sets label to display message JTextField inputBox = new JTextField(40); //sets text field JButton runButton = new JButton("Run"); //button that starts program Container con = getContentPane(); //gets container public void init() { con.setLayout(new FlowLayout());//sets flowlayout con.add(new JLabel()); //jlabel container con.add(inputBox); //input box container con.add(runButton); //run button container con.add(passwordLabel); //password label container con.add(passwordLabel2); //password label container runButton.addActionListener(this);//looks to see if run is clicked inputBox.addActionListener(this);//looks to see if input box is used } public void actionPerformed(ActionEvent e) { String userInput = inputBox.getText(); //gets input from user } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload