“Char cannot be dereferenced” error

后端 未结 4 2247
忘掉有多难
忘掉有多难 2020-12-08 23:19

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

相关标签:
4条回答
  • 2020-12-08 23:52

    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)) { //... }
    
    0 讨论(0)
  • 2020-12-09 00:00

    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).

    0 讨论(0)
  • 2020-12-09 00:02

    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)) {
    
    0 讨论(0)
  • 2020-12-09 00:09

    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)) {
    
    } 
    
    0 讨论(0)
提交回复
热议问题