0

Within this method the String buildWord is used at if(this.guessedLetters.contains(buildWord)), when i run the testProgram with the main file, i get the incorrect results, but when I manually use if(this.guessedLetters.contains("" + letter)), it works. My question is what is the difference here?? Why are these two not the same: if(this.guessedLetters.contains(buildWord)), and if(this.guessedLetters.contains("" + letter))

public String hiddenWord() {
    char letter = ' ';
    String hiddenWord = "";
    String buildWord = "" + letter;
    int i = 0;
    while (i < this.word.length()) {
        letter = this.word.charAt(i);
        if (this.guessedLetters.contains(buildWord))  { // **********
            hiddenWord += letter;
        } else  {
            hiddenWord += '_';
        }
        i++;
    }
    return hiddenWord;
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Y Sang
  • 23
  • 1
  • 1
  • 4
  • the values are from the main method, the values are "A" "D" "S" "F" "D", the word used from a tutorial KISSA – Y Sang May 23 '17 at 22:04

2 Answers2

2

The difference is that letter gets updated in the loop, but buildWord doesn't and always is equal to the string " ". If you add buildWord = "" + letter; after you re-assign letter, then it will work:

while (i < this.word.length()) {
    letter = this.word.charAt(i);
    buildWord = "" + letter;           // <------------
    if (this.guessedLetters.contains(buildWord))

For more information on the different ways to convert a character to a string see How to convert a char to a String?

clcto
  • 9,530
  • 20
  • 42
  • After changing it that way it works, I'm just still confuse how, char letter holds the updated value, how come **`String buildWord = " " + letter;`** doesn't get updated as well? For example: if **`char letter = 'K';`** gets updated, how come, **`buildWord = " " + letter;`** isn't **`buildWord = " " + 'K';`** and so forth as char letter gets updated? – Y Sang May 23 '17 at 22:11
  • No. When you set `buildWord = "" + letter`, it gets the **current value** of `letter` and concatenates it with the empty string. It doesn't keep updating `buildWord` as `letter` changes. What if `"" + letter` was more complex such as getting a value from a database: `String buildWord = GetDatabaseValue() + letter;` do you still think we should go and update `buildWord` when `letter` changes? How would it update, by going and fetching the new database value or using the old cached value? What if `GetDatabaseValue()` changes? How would it even know? You need to tell the computer what to do. – clcto May 24 '17 at 15:12
  • In your example, you are telling the computer to recalculate the value passed into `contains` by calling it with the expression `"" + letter`. – clcto May 24 '17 at 15:15
0

Difference is String.contains(CharSequence s) method will take only CharSequence as a parameter. CharSequence is an interface and all Known Implementing Classes are:

CharBuffer, Segment, String, StringBuffer, StringBuilder

you can pass any one of the above class as a parameter but not directly the char that's why when you are trying to convert letter(char) as a buildWord(String) it accepts as it is an implementation class of CharSequence.

for further reference CharSequence java documentation and String java documentation

Gokul Raj
  • 26
  • 3