Note that there are some explanatory texts on larger screens.

plurals
  1. POEfficent way to replace underscore with char or string
    primarykey
    data
    text
    <p>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:</p> <pre><code> 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(); </code></pre> <p>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.</p> <p>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:</p> <pre><code>for(i = 0; i &lt; GuessWord.length(); i++) { if (GuessWord.charAt(i) == guess2) { mainword.replace(i,i,strGuess.toUpperCase()); } </code></pre> <p>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.</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