char cannot be dereferenced

Discussion in 'Java' started by haig, Jan 10, 2006.

  1. haig

    haig Guest

    Hello

    Ik get an error on this piece of code:

    if((word.charAt(i)).equals("a"){
    .....
    }

    Error: char cannot be dereferenced

    Can someone tell me what's wrong? Or how can I compare each letter of the
    word to the "a"?

    Thanks
    haig, Jan 10, 2006
    #1
    1. Advertising

  2. haig

    James Westby Guest

    haig wrote:
    > Hello
    >
    > Ik get an error on this piece of code:
    >
    > if((word.charAt(i)).equals("a"){
    > ....
    > }
    >
    > Error: char cannot be dereferenced
    >
    > Can someone tell me what's wrong? Or how can I compare each letter of the
    > word to the "a"?
    >
    > Thanks



    Try

    ..equals('a'){

    becomes

    == 'a'){


    James
    James Westby, Jan 10, 2006
    #2
    1. Advertising

  3. haig

    VisionSet Guest

    "haig" <> wrote in message
    news:mEUwf.93178$-ops.be...
    > Hello
    >
    > Ik get an error on this piece of code:
    >
    > if((word.charAt(i)).equals("a"){
    > ....
    > }
    >
    > Error: char cannot be dereferenced


    word.charAt(i) returns a char primitive, you can not call methods on a
    primitive ie equals(String str)
    For that matter you must make the two objects of the same type to make
    equals meaningful.

    so

    objectOneOfTypeA.equals(objectTwoOfTypeA) // is okay

    to modify your example

    char chrPrim = word.charAt(i);
    Character chrObject = Character.valueOf(chrPrim);
    boolean isEqual = Character.valueOf('a').equals(chrObject);

    but since you have a primitive it is easier to just do

    if ( word.charAt(i) == 'a' ) {...} // !!

    --
    Mike W
    VisionSet, Jan 10, 2006
    #3
  4. haig

    haig Guest

    "VisionSet" <> wrote in news:WOUwf.32187$yu.5572
    @newsfe6-gui.ntli.net:


    > word.charAt(i) returns a char primitive, you can not call methods on a
    > primitive ie equals(String str)
    > For that matter you must make the two objects of the same type to make
    > equals meaningful.
    >
    > so
    >
    > objectOneOfTypeA.equals(objectTwoOfTypeA) // is okay
    >
    > to modify your example
    >
    > char chrPrim = word.charAt(i);
    > Character chrObject = Character.valueOf(chrPrim);
    > boolean isEqual = Character.valueOf('a').equals(chrObject);
    >
    > but since you have a primitive it is easier to just do
    >
    > if ( word.charAt(i) == 'a' ) {...} // !!
    >


    Thanks

    And if I want to compare a string of vouwels

    String [] vouwels = {"A", "a", "E", "e", "U", "u", "I", "i", "O", "o"};

    if(word.charAt(i) == vouwels[j]){} //?

    So I need to count the vouwels in a word...
    haig, Jan 10, 2006
    #4
  5. haig

    Roedy Green Guest

    On Tue, 10 Jan 2006 20:25:45 GMT, James Westby <>
    wrote, quoted or indirectly quoted someone who said :

    >> if((word.charAt(i)).equals("a"){


    You have two problems. equals is for comparing objects; == is for
    comparing primitives. You have primitives. Secondly your () don't
    balance.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Jan 10, 2006
    #5
  6. haig

    James Westby Guest

    haig wrote:
    > "VisionSet" <> wrote in news:WOUwf.32187$yu.5572
    > @newsfe6-gui.ntli.net:
    >
    >
    >
    >>word.charAt(i) returns a char primitive, you can not call methods on a
    >>primitive ie equals(String str)
    >>For that matter you must make the two objects of the same type to make
    >>equals meaningful.
    >>
    >>so
    >>
    >>objectOneOfTypeA.equals(objectTwoOfTypeA) // is okay
    >>
    >>to modify your example
    >>
    >>char chrPrim = word.charAt(i);
    >>Character chrObject = Character.valueOf(chrPrim);
    >>boolean isEqual = Character.valueOf('a').equals(chrObject);
    >>
    >>but since you have a primitive it is easier to just do
    >>
    >> if ( word.charAt(i) == 'a' ) {...} // !!
    >>

    >
    >
    > Thanks
    >
    > And if I want to compare a string of vouwels
    >
    > String [] vouwels = {"A", "a", "E", "e", "U", "u", "I", "i", "O", "o"};
    >
    > if(word.charAt(i) == vouwels[j]){} //?
    >
    > So I need to count the vouwels in a word...
    >

    With proper looping and counting that could do it, yes. Take a look at
    ..toCharArray() method of string, to save repeatedly extracting the same
    characters out of the string, then it's just a case of looping over the
    two arrays and incrementing a count when the values match.

    James
    James Westby, Jan 10, 2006
    #6
  7. haig

    Roedy Green Guest

    On Tue, 10 Jan 2006 20:39:36 GMT, haig <> wrote,
    quoted or indirectly quoted someone who said :

    >String [] vouwels = {"A", "a", "E", "e", "U", "u", "I", "i", "O", "o"};
    >
    >if(word.charAt(i) == vouwels[j]){} //?


    You would need a nested loop over the chars in word and the possible
    vowels and increment a counter when you find a match.

    The for:each is neat for this:

    for ( char vowel : vowels )

    But you need to use a char[] instead of a String[].

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Jan 10, 2006
    #7
  8. haig wrote:
    > Hello
    >
    > Ik get an error on this piece of code:
    >
    > if((word.charAt(i)).equals("a"){
    > ....
    > }
    >
    > Error: char cannot be dereferenced
    >
    > Can someone tell me what's wrong? Or how can I compare each letter of the
    > word to the "a"?
    >
    > Thanks


    Seems that charAt() returns a char. A char does not have a method named
    equals.

    Try

    if (word.charAt(i) == 'a') {
    ....
    }
    Malte Christensen, Jan 10, 2006
    #8
  9. haig

    Roedy Green Guest

    On Tue, 10 Jan 2006 20:18:26 GMT, haig <> wrote,
    quoted or indirectly quoted someone who said :

    >if((word.charAt(i)).equals("a"){


    compare Strings with Strings and chars with chars. You have char on
    the left and String on the right.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Jan 10, 2006
    #9
  10. "haig" <> wrote:
    > And if I want to compare a string of vouwels
    >
    > String [] vouwels = {"A", "a", "E", "e", "U", "u", "I", "i", "O", "o"};
    >
    > if(word.charAt(i) == vouwels[j]){} //?
    >
    > So I need to count the vouwels in a word...
    >

    The compiler will give an error, because you try to compare char with
    String.

    What you probably want to compare char wit char:
    char [] vouwels = {'A', 'a', 'E', 'e', 'U', 'u', 'I', 'i', 'O', 'o'};

    if(word.charAt(i) == vouwels[j]){}

    --
    "TFritsch$t-online:de".replace(':','.').replace('$','@')
    Thomas Fritsch, Jan 10, 2006
    #10
  11. haig

    James Westby Guest

    James Westby wrote:
    > haig wrote:
    >
    >> "VisionSet" <> wrote in news:WOUwf.32187$yu.5572
    >> @newsfe6-gui.ntli.net:
    >>
    >>
    >>
    >>> word.charAt(i) returns a char primitive, you can not call methods on a
    >>> primitive ie equals(String str)
    >>> For that matter you must make the two objects of the same type to make
    >>> equals meaningful.
    >>>
    >>> so
    >>>
    >>> objectOneOfTypeA.equals(objectTwoOfTypeA) // is okay
    >>>
    >>> to modify your example
    >>>
    >>> char chrPrim = word.charAt(i);
    >>> Character chrObject = Character.valueOf(chrPrim);
    >>> boolean isEqual = Character.valueOf('a').equals(chrObject);
    >>>
    >>> but since you have a primitive it is easier to just do
    >>>
    >>> if ( word.charAt(i) == 'a' ) {...} // !!
    >>>

    >>
    >>
    >> Thanks
    >>
    >> And if I want to compare a string of vouwels
    >> String [] vouwels = {"A", "a", "E", "e", "U", "u", "I", "i", "O", "o"};
    >> if(word.charAt(i) == vouwels[j]){} //?
    >>
    >> So I need to count the vouwels in a word...
    >>

    > With proper looping and counting that could do it, yes. Take a look at
    > .toCharArray() method of string, to save repeatedly extracting the same
    > characters out of the string, then it's just a case of looping over the
    > two arrays and incrementing a count when the values match.
    >
    > James


    As Roedy pointed out you need a char[] not String[] of vowels. This
    applies to this method as well.

    James
    James Westby, Jan 10, 2006
    #11
  12. Thomas Fritsch wrote:
    > "haig" <> wrote:
    >
    >>And if I want to compare a string of vouwels
    >>
    >>String [] vouwels = {"A", "a", "E", "e", "U", "u", "I", "i", "O", "o"};
    >>
    >>if(word.charAt(i) == vouwels[j]){} //?
    >>
    >>So I need to count the vouwels in a word...
    >>

    >
    > The compiler will give an error, because you try to compare char with
    > String.
    >
    > What you probably want to compare char wit char:
    > char [] vouwels = {'A', 'a', 'E', 'e', 'U', 'u', 'I', 'i', 'O', 'o'};


    Or, easier to read:

    char[] vowels = "AaEeIiOoUu".toCharArray();

    Tom Hawtin
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
    Thomas Hawtin, Jan 10, 2006
    #12
  13. haig

    Roedy Green Guest

    On Tue, 10 Jan 2006 21:54:09 +0100, Malte Christensen
    <> wrote, quoted or indirectly quoted
    someone who said :

    >Seems that charAt() returns a char. A char does not have a method named
    >equals.


    In fact, because char is a primitive, it does not have ANY methods.
    Only Objects have methods, e.g. Character or String.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Jan 10, 2006
    #13
  14. "Roedy Green" <> wrote in
    message news:p...
    > On Tue, 10 Jan 2006 20:39:36 GMT, haig <> wrote,
    > quoted or indirectly quoted someone who said :
    >
    >>String [] vouwels = {"A", "a", "E", "e", "U", "u", "I", "i", "O", "o"};
    >>
    >>if(word.charAt(i) == vouwels[j]){} //?

    >
    > You would need a nested loop over the chars in word and the possible
    > vowels and increment a counter when you find a match.
    >
    > The for:each is neat for this:
    >
    > for ( char vowel : vowels )
    >
    > But you need to use a char[] instead of a String[].


    Simpler, though is:

    int index = "AaEeIiOoUu".indexOf (word.charAt(i));
    if (index >= 0) ...
    Mike Schilling, Jan 11, 2006
    #14
  15. Roedy Green wrote:
    > On Tue, 10 Jan 2006 21:54:09 +0100, Malte Christensen
    > <> wrote, quoted or indirectly quoted
    > someone who said :
    >
    >
    >>Seems that charAt() returns a char. A char does not have a method named
    >>equals.

    >
    >
    > In fact, because char is a primitive, it does not have ANY methods.
    > Only Objects have methods, e.g. Character or String.

    Tnx for stating the obvious, I had left it to the OP.
    Malte Christensen, Jan 11, 2006
    #15
  16. It sounds like you may even want to look at regular expressions to do
    your test...
    java_programmer, Jan 11, 2006
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. matt
    Replies:
    10
    Views:
    3,313
    Grant Wagner
    Sep 2, 2005
  2. B. Penn
    Replies:
    6
    Views:
    547
    Old Wolf
    Aug 9, 2004
  3. lovecreatesbeauty
    Replies:
    1
    Views:
    1,012
    Ian Collins
    May 9, 2006
  4. Replies:
    7
    Views:
    940
    Bo Persson
    Oct 30, 2006
  5. justineee
    Replies:
    1
    Views:
    1,052
    Mark Space
    Oct 24, 2008
Loading...

Share This Page