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 8792049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:55:34+00:00 2026-06-13T22:55:34+00:00

I have researched this topic for a while, but without much success. I did

  • 0

I have researched this topic for a while, but without much success. I did find the StringBuilder and it works wonders, but that’s as far as I got. Here is how I got my hangman program to work like it should:

  if(strGuess.equalsIgnoreCase("t")){
        mainword.replace(0,1,"T");
        gletters.append('T');      
    }
        else if(strGuess.equalsIgnoreCase("e")){
        mainword.replace(1,2,"E");
        gletters.append('E');       
    }
        else if(strGuess.equalsIgnoreCase("c")){
        mainword.replace(2,3,"C");
        gletters.append('C');         
    }
        else if(strGuess.equalsIgnoreCase("h")){
        mainword.replace(3,4,"H");
        gletters.append('H');      
    }
        else if(strGuess.equalsIgnoreCase("n")){
        mainword.replace(4,5,"N");
        gletters.append('N');      
    }
        else if(strGuess.equalsIgnoreCase("o")){
        mainword.replace(5,6,"O");
        mainword.replace(7,8,"O");
        gletters.append('O');       
    }
        else if(strGuess.equalsIgnoreCase("l")){
        mainword.replace(6,7,"L");
        gletters.append('L');       
    }
         else if(strGuess.equalsIgnoreCase("g")){
        mainword.replace(8,9,"G");
        gletters.append('G');      
    }
        else if(strGuess.equalsIgnoreCase("y")){
        mainword.replace(9,10,"Y");
        gletters.append('Y'); 
    }
        else{
        JOptionPane.showMessageDialog(null, "Sorry, that wasn't in the word!");
        errors++;
        gletters.append(strGuess.toUpperCase());
    }
        SetMain = mainword.toString();
        GuessedLetters = gletters.toString();
        WordLabel.setText(SetMain);
        GuessedLabel.setText(GuessedLetters);
        GuessText.setText(null);
        GuessText.requestFocusInWindow();

However, I can’t do this for EVERY letter for EVERY word, so is there a simple and efficient way to do this? What I want is to have a loop of some sort so that I would only have to use it once for whatever word. So the word could be technology (like it is above) or apple or pickles or christmas or hello or whatever.

I have tried using a for loop, and I feel the answer lies in that. And if someone could explain the charAt() method and how/where to use it, that’d be good. The closest I got to being more efficient is:

for(i = 0; i < GuessWord.length(); i++) { 
    if (GuessWord.charAt(i) == guess2) { 
        mainword.replace(i,i,strGuess.toUpperCase()); 
    }

So if you could use that as a basis and go off of it, like fix it? Or tell me something I haven’t thought of.

  • 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-13T22:55:35+00:00Added an answer on June 13, 2026 at 10:55 pm

    It’s a good question. There’s clearly repeated code, so how do you replace all that with something reusable. Actually, you can dispense with all of your code.

    That whole code block can be replaced by just one line (that works for every word)!

    String word = "TECHNOLOGY"; // This is the word the user must guess
    mainword = word.replaceAll("[^" + gletters + "]", "_");
    

    This uses replaceAll() with a regex that means “any letter not already guessed” and replaces it with a underscore character "_". Note that Strings are immutable, and the replaceAll() method returns the modified String – it doesn’t modify the String called on.

    Here’s some test code to show it in action:

    public static void main(String[] args) {
        String word = "TECHNOLOGY"; // what the user must guess
        StringBuilder gletters = new StringBuilder("GOTCHA"); // letters guessed
        String mainword = word.replaceAll("[^" + gletters + "]", "_");
        System.out.println(mainword);
    }
    

    Output:

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

Sidebar

Related Questions

I have researched this topic, but am afraid I have too little experience with
I've researched a bit into this topic and have discovered that the last time
I have researched this topic for months now with no success. Many posts say
I have researched enough on this topic sans any luck :-( My requirement is
Have done some research into this topic, but found no relevant answers. What I
I have tried to do research on this topic, but have not found an
I already did a lot of research on this topic and have implemented a
I have research alot on this topic, but no clue .. I am downloading
I have researched the topic further and i realise that for an example to
Ok, I have done plenty of research on this topic before asking the question,

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.