12

I'm trying to use the char method isLetter(), which is supposed to return boolean value corresponding to whether the character is a letter. But when I call the method, I get an error stating that "char cannot be dereferenced." I don't know what it means to dereference a char or how to fix the error. the statement in question is:

if (ch.isLetter()) 
{
....
....
}

Any help? What does it mean to dereference a char and how do I avoid doing so?

amesh
  • 1,311
  • 3
  • 21
  • 51
user658168
  • 1,215
  • 4
  • 15
  • 15

4 Answers4

26

The type char is a primitive -- not an object -- so it cannot be dereferenced

Dereferencing is the process of accessing the value referred to by a reference. Since a char is already a value (not a reference), it can not be dereferenced.

use Character class:

if(Character.isLetter(c)) {
manji
  • 47,442
  • 5
  • 96
  • 103
  • 1
    +1 - though it should also be noted that the `.` in `Character.isLetter(c)` denotes the use of a static method of `Character`. – Stephen C Apr 03 '11 at 03:34
2

A char doesn't have any methods - it's a Java primitive. You're looking for the Character wrapper class.

The usage would be:

if(Character.isLetter(ch)) { //... }
no.good.at.coding
  • 20,221
  • 2
  • 60
  • 51
1

I guess ch is a declared as char. Since char is a primitive data type and not and object, you can't call any methof from it. You should use Character.isLetter(ch).

MByD
  • 135,866
  • 28
  • 264
  • 277
-1

If Character.isLetter(ch) looks a bit wordy/ugly you can use a static import.

import static java.lang.Character.*;


if(isLetter(ch)) {

} else if(isDigit(ch)) {

} 
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130