Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8799017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:13:28+00:00 2026-06-14T00:13:28+00:00

I’m writing a Java hangman game, and so far I’ve gotten the code to

  • 0

I’m writing a Java hangman game, and so far I’ve gotten the code to replace one at a time. What I need is it to replace each time until the word is guessed. Here’s how it works one at a time:

        for(int i = 0; i < GuessWord.length(); i++) {
        foundword = words[randvalue].replaceAll("[^" + xletters + "]", "_ ");
        }
            GuessedLetters = xletters.toString().toUpperCase();
            WordLabel.setText(foundword.toUpperCase());
            GuessedLabel.setText(GuessedLetters);
            GuessText.setText(null);
            GuessText.requestFocusInWindow();

And here’s my other attempt that didn’t work out so well:

       //replace underscores with letters as they are guessed while the word is not solved
        do {
        foundword = words[randvalue].replaceAll("[^" + xletters + "]", "_ ");
        }
        while (!SetMain.equals(GuessWord));
            //set results to labels
            WordLabel.setText(foundword.toUpperCase());
            GuessedLabel.setText(GuessedLetters);
            GuessText.setText(null);
            GuessText.requestFocusInWindow();

And here’s my entire code for those who need it:

    import java.util.Random;
    import java.util.Scanner;
    import javax.swing.JOptionPane;

    public class MainFrame extends javax.swing.JFrame {

        public MainFrame() {
            initComponents();
        }
    static String SecretWord = "";
    double Result = 0;
    StringBuilder mainword = new StringBuilder();
     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];
    int errors = 0;

        private void GoButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          

        for(int i = 0; i < GuessWord.length(); i++) { 
           mainword.append("_ ");
        }
           mainword.append(SecretWord);
           String SetMain = mainword.toString();
           WordLabel.setText(SetMain);
           GuessButton.setEnabled(true);
           GoButton.setEnabled(false); 
        }                                        

        private void GuessButtonActionPerformed(java.awt.event.ActionEvent evt){                                            
        String strGuess = GuessText.getText(); //user input
        StringBuilder xletters = new StringBuilder(strGuess); // letters guessed
        String GuessedLetters = null;
        String foundword = null;

        //replace underscores with letters as they are guessed
        for(int i = 0; i < GuessWord.length(); i++) {
        foundword = words[randvalue].replaceAll("[^" + xletters + "]", "_ ");

        }
            //set to labels to display results
            GuessedLetters = xletters.toString().toUpperCase();
            WordLabel.setText(foundword.toUpperCase());
            GuessedLabel.setText(GuessedLetters);
            GuessText.setText(null);
            GuessText.requestFocusInWindow();

So I want the code to be able to replace letters cumulatively, i.e. if the word was hello:

Secret Word: _ _ _ _ _
guessed e….

Secret Word: _ e _ _ _
guessed o….

Secret Word: _ e _ _ o
guessed a….

Secret Word: _ e _ _ o
guessed h….

Seret Word: h e _ _ o
guessed l….

Secret Word: h e l l o
Congrats!

I need this to work in Netbeans IDE 7.2, and it has to work for a JLayeredPane, not the System.out.print method. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T00:13:29+00:00Added an answer on June 14, 2026 at 12:13 am

    I’m not sure if this is entirly what you after, but it might give you some ideas…

    public class TestHangMan {
    
        public static void main(String[] args) {
            new TestHangMan();
        }
    
        public TestHangMan() {
            String word = "Testing";
            String mask = "_______";
    
            mask = checkGuess(word, mask, 'a');
            System.out.println(mask);
            mask = checkGuess(word, mask, 't');
            System.out.println(mask);
            mask = checkGuess(word, mask, 'e');
            System.out.println(mask);
            mask = checkGuess(word, mask, 's');
            System.out.println(mask);
            mask = checkGuess(word, mask, 'g');
            System.out.println(mask);
            mask = checkGuess(word, mask, 'n');
            System.out.println(mask);
            mask = checkGuess(word, mask, 'i');
            System.out.println(mask);
        }
    
        protected String checkGuess(String word, String mask, char guess) {
            guess = Character.toLowerCase(guess);
            StringBuilder sb = new StringBuilder(mask);
            for(int index = 0; index < word.length(); index++) {
                if (Character.toLowerCase(word.charAt(index)) == guess) {
                    sb.setCharAt(index, word.charAt(index));
                }
            }
            return sb.toString();
        }
    
    }
    

    Which produces…

    _______
    T__t___
    Te_t___
    Test___
    Test__g
    Test_ng
    Testing
    

    You can check the mask to see if the user has already made a guess simply by using mask.toLowerCase().contains(stringGuess.toLowerCase())

    Hope it helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
I need to clean up various Word 'smart' characters in user input, including but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.