Array Required, but java.lang.String found

You can’t use array subscripts: [], to index into a String, and both hidden and original are Strings.

You can instead use original.charAt(i) to read a character at an index.

As for writing a character at an index: java Strings are immutable, so you can’t change individual characters. Instead make hidden a StringBuilder, or simply a char[]:

// in your class member declarations:
char hidden[] = createHidden();

// possible implementation of createHidden:
char[] createHidden()
    if (original != null)
        return new char[original.length()];
    else return null;

And then your loop can use original.charAt like so:

if (this.original.charAt(i) == input)
    found = true;
    this.hidden[i] = this.original.charAt(i);

Leave a Comment