Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Hey Jordan, your first attempt looks very good! You only need some more logic inside the while loop to read guesses and replace "*"s with correct guesses. And I would advise you to store the obfuscated word ("*****...") in a string also instead of just printing it out, will be handy later on..</p> <p>Judging by your code you don't need any help on user input, your only problem is correct replacing of stars with right guesses, lets get to it:</p> <pre><code>String secret; //read in secret string String displaySecret; //generate as many "*"s as secret is long and store them in displaySecret </code></pre> <p>Now the cool thing is this:</p> <blockquote> <p>...no repeated letters...</p> </blockquote> <p>which will make your assignment much easier! Look at the documentation of the String class provided by Williwaw. There you'll find two methods with which will lead to the solution:</p> <ul> <li>One method finds the first occurrence of a character inside a string and outputs its position. And since you don't accept duplicate letters, that'll also be the only occurrence.</li> <li>The other method can replace the character at a given position inside a string with another character.</li> </ul> <p>I think from that you'll find the solution easily. Feel free to ask further questions in the comments!</p> <p><strong>EDIT: Some more help</strong><br/></p> <pre><code>String secret = "example-text"; String displaySecret = ""; for (int i = 0; i &lt; secret.length(); i++) displaySecret += "*"; char guess; //read in a guess int position = secret.indexOf(guess); //now position contains the index of guess inside secret, or //-1 if the guess was wrong String newDisplaySecret = ""; for (int i = 0; i &lt; secret.length(); i++) if (i == position) newDisplaySecret += secret.charAt(i); //newly guessed character else newDisplaySecret += displaySecret.charAt(i); //old state displaySecret = new String(newDisplaySecret); </code></pre> <p>Damn I was sure there was some kind of setCharAt(int) method.. the loop does the job.</p>
 

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