Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > char cannot be dereferenced

Reply
Thread Tools

char cannot be dereferenced

 
 
haig
Guest
Posts: n/a
 
      01-10-2006
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
 
Reply With Quote
 
 
 
 
James Westby
Guest
Posts: n/a
 
      01-10-2006
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
 
Reply With Quote
 
 
 
 
VisionSet
Guest
Posts: n/a
 
      01-10-2006

"haig" <(E-Mail Removed)> wrote in message
news:mEUwf.93178$(E-Mail Removed)-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


 
Reply With Quote
 
haig
Guest
Posts: n/a
 
      01-10-2006
"VisionSet" <(E-Mail Removed)> 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...

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      01-10-2006
On Tue, 10 Jan 2006 20:25:45 GMT, James Westby <(E-Mail Removed)>
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.
 
Reply With Quote
 
James Westby
Guest
Posts: n/a
 
      01-10-2006
haig wrote:
> "VisionSet" <(E-Mail Removed)> 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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      01-10-2006
On Tue, 10 Jan 2006 20:39:36 GMT, haig <(E-Mail Removed)> 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.
 
Reply With Quote
 
Malte Christensen
Guest
Posts: n/a
 
      01-10-2006
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') {
....
}
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      01-10-2006
On Tue, 10 Jan 2006 20:18:26 GMT, haig <(E-Mail Removed)> 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.
 
Reply With Quote
 
Thomas Fritsch
Guest
Posts: n/a
 
      01-10-2006
"haig" <(E-Mail Removed)> 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('$','@')


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"void cannot be dereferenced" error.. justineee Java 1 10-24-2008 05:08 AM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
char cannot be dereferenced matt Java 10 09-02-2005 01:59 PM
Pointer: why could a deleted pointer be dereferenced? B. Penn C++ 6 08-09-2004 10:27 PM



Advertisments