4
In a test code (in Java), whose goal was to convert a string in one another string between upper and lower case letters, I received the following error at the time of compilation:
error: char cannot be dereferenced
result = Letter.toLowerCase();
Following the example code:
public void solve(String text) {
int len = text.length();
StringBuffer buffer = new StringBuffer(len);
for(int i=0; i<len; i++){
char letter = text.charAt(i);
char result;
if (i % 2 == 0)
result = letter.toUpperCase(); //error: compile time
else
result = letter.toLowerCase(); //error: compile time
buffer.append(result);
}
System.out.println(buffer.toString());
}
solve("Teste");
// Resultado esperado:
// TeStE
Why the mistake happens and what it means?
In that case you’re not using
Character
as a wrapper, simply using static class methodsCharacter
. If I were to use as wrapper, in many cases the autobox / autounbox would be in charge of the conversion (although I don’t think this applies to calling methods). However, the classCharacter
only have these methods that matter as static anyway, so your approach is correct.– mgibsonbr
@mgibsonbr You’re right. I corrected the answer.
– Piovezan