Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'd try a simpler approach, in which you hold an array of characters that you can change based on an input. This is an overal description of the idea:</p> <pre><code>char guess[] = {'_', '_', '_', '_', '_', ' ', '_', '_', '_', '_', '_'}; // It's "Hello World" :) if(input == answer.getCharAt(inputIndex)) guess[inputIndex] = answer.getCharAt(inputIndex); //replace the underscore with the correct character else //Add it to a list of used characters, ignore it or do as you've been asked to </code></pre> <p>In your case, create this <code>guess</code> array by invoking <code>answer.toCharArray()</code> where <code>answer</code> is the string that has the correct word. You'll want to replace all the characters in that string with underscores by using <code>replaceAll("[A-Za-z0-9]", "_")</code> (just remember to keep the original answer too). The first parameter is a regular expression that means "The group of All characters from A to Z, from a to z and from 0 to 9", you'll want to check up some <a href="http://en.wikipedia.org/wiki/Regular_expression" rel="nofollow">Regular Expressions</a> to get used to them, and trust me, you'll use them more sooner than later.</p> <p>Then, you'll just want to replace <code>guess[i]</code> with <code>answer.getCharAt(i)</code> everytime a guess is done correctly, avoiding the tedious work of building a string character by character which can be tricky.</p> <p>This is a brief description of a different approach, since it's an assigment you can decide if you want to try it or not and the general implementation is up to you. I'll just describe an idea.</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.
    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