Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Hangman - replacing underscore with character
    primarykey
    data
    text
    <p>So I'm creating a hangman game for my java class, but I'm having troubles replacing the underscores in the word with letters. I got it to print out the word in underscores like:</p> <pre><code> for(int i = 0; i &lt; GuessWord.length(); i++) { if (guesses[GuessWord.charAt(i) - 'a']) { mainword.append(words[i].charAt(i)); } else { mainword.append("_"); } mainword.append(" "); } </code></pre> <p>The rest is the rest of my code. I should mention I'm working in Netbeans IDE 7.2 and I'm using a JLayeredPane to display everything, not System.out.print. Thanks!</p> <pre><code> import java.util.Random; import java.util.Scanner; import javax.swing.JOptionPane; public class MainFrame extends javax.swing.JFrame { public MainFrame() { initComponents(); } //declare variables static String SecretWord = ""; static String Letters = ""; double Result = 0; String SetMain = null; StringBuilder mainword = new StringBuilder(); StringBuilder gletters = new StringBuilder(); boolean[] guesses = new boolean[26]; String[] words = {"technology", "computer", "camera", "graphic design", "digital", "media", "technician", "photography", "troubleshoot", "pixels", "application", "download"}; Random r = new Random(); int randvalue = r.nextInt(11); String GuessWord = words[randvalue]; private void GoButtonActionPerformed(java.awt.event.ActionEvent evt) { mainword.append(SecretWord); //make word in underscore form for(int i = 0; i &lt; GuessWord.length(); i++) { if (guesses[GuessWord.charAt(i) - 'a']) { mainword.append(words[i].charAt(i)); } else { mainword.append("_"); } mainword.append(" "); } //put in label SetMain = mainword.toString(); WordLabel.setText(SetMain); GuessButton.setEnabled(true); GoButton.setEnabled(false); } private void GuessButtonActionPerformed(java.awt.event.ActionEvent evt) { //declare variables String strGuess = GuessText.getText(); String SetMain = null; String GuessedLetters = null; Result = 1;//(int)(Math.random() * 11) + 1; int errors = 0; int i = 0; char guess2 = strGuess.charAt(i); gletters.append(Letters); //*******MAJOR PROBLEM AREA FOCUS HERE******* do{ //replace underscore with guessed letter for(i = 0; i &lt; GuessWord.length(); i++) { if (GuessWord.charAt(i) == guess2) { mainword.replace(0,i,strGuess.toUpperCase()); } else { mainword.append("_"); } mainword.append(" "); } //put in labels SetMain = mainword.toString(); GuessedLetters = gletters.toString(); WordLabel.setText(SetMain); GuessedLabel.setText(GuessedLetters); GuessText.setText(null); GuessText.requestFocusInWindow(); }//end of do while(SetMain == null); if (SetMain.equalsIgnoreCase(GuessWord)){ //show winning message to user and reset game JOptionPane.showMessageDialog(null, "Congrats!"); GuessButton.setEnabled(false); GoButton.setEnabled(true); WordLabel.setText(null); GuessedLabel.setText(null); WinsLabel.setText("1"); } //if too many errors show lost message else if (errors &gt;= 5){ JOptionPane.showMessageDialog(null, "You Lost!"); GuessButton.setEnabled(false); GoButton.setEnabled(true); WordLabel.setText(null); GuessedLabel.setText(null); LossesLabel.setText("1"); } }//end of 1GAME } </code></pre> <p>Any help would be great! Please nothing too complicated. Also I marked where my major problem area is in the above code.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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