Reputation: 129
I want to make a code that outputs everything in the string, "thing", as underscore except the letters that have the letter stored in guessChar. Currently, the code adds on top of itself. Can anyone help me with this logic error? Currently, the code gives a long strings of underscores. The desired output is "_e___ _____ E".
String thing = "Hello World E";
String phrase = "";
String guessChar = "e";
String phraseCorrectGuessList = "";
boolean l = true;
String alphabet = "abcdefghijklmnopqrstuvwxyz";//String that holds the alphabet that is later used as indexing
phrase = thing.toLowerCase();//String that holds the phrase (lower case)
String hiddenPhrase = "";//String that holds the parts of the phrase that has not yet been unvealed
String phraseOnlyChar = "";//String that holds phrase without all the punctuation
String hiddenPhraseOnlyChar = "";//String that hold the parts of the phrase that has not yet been unvealed without all the punctuation
for (int i = 0; i < thing.length(); i++){
for (int pos = 0; pos < phrase.length(); pos++){//for loop that checks if there is a space or not
if (alphabet.indexOf(phrase.charAt(pos)) >= 0){//if there is a letter
hiddenPhrase = hiddenPhrase + "_ ";//saves to hidden phrase (changes letters to underscrore)
phraseOnlyChar += phrase.charAt(pos);
}//end of if
else{
hiddenPhrase = hiddenPhrase + phrase.charAt(pos) + " ";//saves area as punctuation
}//end of else
}//end of for loop
if (phrase.indexOf(guessChar) >= 0){//if user enters in a consonant that is in the phrase
System.out.println("Congratulations, you are correct");//prints out that the user guessed correctly
phraseCorrectGuessList += guessChar;//adds character to correctly guessed letters string
System.out.println(hiddenPhrase);//prints the phrase that is hidden
}
for (int count = 0; count < phrase.length(); count++){//for loop that goes through every letter in the phrase checks to see if the user inputted character is any character in the phrase
if (alphabet.indexOf(phrase.charAt(count)) >= 0 && phraseCorrectGuessList.indexOf(phrase.charAt(count)) < 0){//if user inputted character isn't in the phrase
hiddenPhrase = hiddenPhrase + "_ ";//saves hidden phrase as is
}//end of if
else{//if user inputted character is in the phrase
hiddenPhrase = hiddenPhrase + phrase.charAt(count) + " ";//saves hidden phrase but with all instances of that character revealed
}//end of else
}//end of for loop
for (int index = 0; index < hiddenPhrase.length(); index++){
if (alphabet.indexOf(hiddenPhrase.charAt(index)) >= 0){
hiddenPhraseOnlyChar += hiddenPhrase.charAt(index);
}
}
}
Upvotes: 0
Views: 146
Reputation: 3007
There is actually a much more easier way to do this using,
public static void main(String[] args) {
String thing = "Hello World E";
String phrase = "";
char guessChar = 'e';
String finalstr = "";
phrase = thing.toLowerCase();
for (int i = 0; i < thing.length(); i++){
char test = phrase.charAt(i);
if (test == ' ')
{
finalstr += " ";
}
else if (test == guessChar)
{
finalstr += thing.charAt(i);
}
else
{
finalstr += "_";
}
}
System.out.println(finalstr);
}
Output
_e___ _____ E
Upvotes: 1
Reputation: 425278
You have way too much code for what you're trying to do.
Try this:
String hidden = thing.replaceAll("(?i)[^ " + guessChar + "]", "_");
Breaking down the regex:
(?i)
means "ignore case"[^...]
is a negated character class, ie chars that are not in the list, which in this case is the space cahr and the guess charsThis will work with guessChar
having multiple characters in it.
Here's some test code:
String thing = "Hello World E";
String guessChar = "ewo";
String hidden = thing.replaceAll("(?i)[^ " + guessChar + "]", "_");
System.out.println(hidden);
Output:
_e__o Wo___ E
Upvotes: 1
Reputation: 890
While I'm not 100% what you're asking about your code specifically, I can do what you're asking in your stated question. Consider making code for your questions more concise in the future.
You should have a "current word", as I assume you are making a hangman game. Take that current word, turn it into a character array, and test each character in the array to see if it contains a space. This generates the blanks.
toBeBlankArray = currentWord.toCharArray();
for(int x = 0; x < toBeBlankArray.length; x++)
{
if(toBeBlankArray[x] != ' ')//ensures that spaces don't become underscores
{
toBeBlankArray[x] = '_';
}
}
When the user guesses a letter, you need to take their guess and iterate the array to get a boolean array to test for matches. If you do this in a method, it'll look like this.
public static boolean[] checkArrayForMatches(String currentWord, char charToTest)//this generates a boolean array in response to the user's guess against the current word
{
String upperCaseCurretWord = currentWord.toUpperCase();
char[] currentWordArray = upperCaseCurretWord.toCharArray();//I made this part to avoid confusion due to case sensitivity
boolean[] returnArray = new boolean[currentWordArray.length];
for(int x = 0; x < returnArray.length; x++)
{
char characterInArray = currentWordArray[x];
if(characterInArray == charToTest)
{
returnArray[x] = true;
}
else
{
returnArray[x] = false;
}
}
return returnArray;
}
You then compare your true-false array to the starting word like the following. I called my starting word "toBeBlankArray". You should also make a variable to hold the boolean array for testing the user's guess. I called mine "isArrayRight"
boolean[] isArrayRight = checkArrayForMatches(currentWord, charToSend);
//takes the blanks and fills in the letter if guess was correct
for(int x = 0; x < isArrayRight.length; x++)
{
if(isArrayRight[x] == true)
{
toBeBlankArray[x] = currentWordArray[x];
}
}
You should have to add some conditions and context, but this is the core code to test a character against an array of characters.
Upvotes: 0