Question

I am trying to implement a function to compare between a 5 letters string and an array of char and to match them by comparing every char of the string to every char of the array when I try to use it, it always returns something like [C@bebf1eb

Here's my function:

static String matching(String myWord1, char[] word_taken) {

    char result[] = new char[5];
    int k = 0;
    char[] myWord = myWord1.toCharArray();

    for (int i = 0; i < myWord.length; i++) {
        for (int j = 0; j < word_taken.length; j++) {

            if (myWord[i] == word_taken[j]) {

                result[k] = myWord[i];
                k++;
                break;
            }

        }
    }
    return result.toString();

}
Était-ce utile?

La solution

when I try to use it, it always returns something like [C@bebf1eb

Yes the toString() method for arrays is not overidden so you get the default implementation.

Use the String constructor that takes a char array instead.

return new String(result);


If you want to avoid to recopy the content of the array when creating your String, you could use a StringBuilder and append the char to it when it's needed.

At the end, simply return myStringBuilder.toString();

Autres conseils

Yes I have also face the same problem and get the correct result by creating a string object

return new String(result);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top