I am currently trying to right a rough draft code block for an object oriented hangman assignment I have to tackle. I know that similar questions have been asked but I didn't find a solution for the specific way i'm attempting to write this (which quite possibly is actual shite).
The way the code is intended to work is as follows.
Word_to_guess pulls a word from the array wordBank[] which contains 19 different fruits and 1 pickle (this is just to entertain my professor).
Then, the following While loop checks if "noSuccess" has been falsified, while it remains true the loop asks for character input from the user as a guess.
then the nested for loop iterates through the length of word_to_guess to check if Characters at index i match character c which has been set to userGuess.charAt(0) (in case the user enters more than 1 letter restricting their guess to the first letter in the string they might enter).
now this is where I'm having my issues. I don't know how to use the .replace method to fill in spots with specific characters and store newWord for each iteration of the for loop and have it print a progress report to the user as they guess so that they can keep track of their guesses. I had attempted to use the following statement newWord = newWord.replace(newWord.CharAt(i), c) but this did not work and caused a compiling error stating that the index could not be found. Which i didn't understand and still don't.
the final if statement checks if noSuccess has been falsified which is defined by booleancheck.contentEquals(word_to_guess) booleancheck is defined by newWord without any spaces between characters.
If anyone can lend me a hand in sorting out that portion of the code or any other parts that look iffy I'd greatly appreciate it.
Thank you so much!
Alex
word_to_guess = wordBank[num];
boolean noSuccess = true;
while(noSuccess)
{
System.out.println("Enter your guess: ");
System.out.println("letters used - " + lettersUsed);
userGuess = keyboard.nextLine();
c = userGuess.charAt(0);
for(int i = 0; i < word_to_guess.length(); i++)
{
if(word_to_guess.charAt(i) == c)
{
if(newWord.charAt(i) == ' ')
{
newWord = newWord.replace(' ', c);
}
else if(newWord.charAt(i) == '_')
{
newWord = newWord.replace('_', c);
}
}
else
newWord = newWord.replace(' ', '_');
}
System.out.println(newWord);
counter ++;
lettersUsed = lettersUsed + c;
System.out.println("letters used - " + lettersUsed);
String booleanCheck = newWord.replaceAll("\\s+","");
if(booleanCheck.contentEquals(word_to_guess ) && counter > 1)
{
noSuccess = false;
System.out.println("Great job, you guessed the right letters!");
}
Update #1:
I came up with the following code:
for(int i = 0; i < word_to_guess.length(); i++)
{
if(word_to_guess.charAt(i) == c)
{
strProg.setCharAt(i, c);
}
else
{
strProg.setCharAt(i, '_');
}
newWord = strProg.toString();
}
but i'm not sure what I'm doing wrong with the .toString() method because each iteration replaces the previous progress with "_" instead of being additive.
Update: #2
Edit to the previous update, I read that using StringBuilder in this instance is better than StringBuffer. I also discovered that the append and insert methods are available to the StringBuilder object.
This string object is located outside of the while loop the for loop is in.
String newWord = String.join("", Collections.nCopies(word_to_guess.length(),
" "));
this is inside the while loop.
StringBuilder strProg = new StringBuilder(newWord);
for(int i = 0; i < word_to_guess.length(); i++)
{
if(word_to_guess.charAt(i) == c)
{
strProg.insert(i, c);
}
else if(word_to_guess.charAt(i) != c && newWord.charAt(i) == '_')
{
strProg.delete(i, i);
}
else
{
strProg.insert(i, '_');
}
}
the .insert method is resulting in a much longer string. I don't know what to do now.