Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

char cannot be dereferenced

Thread view: 
haig - 10 Jan 2006 20:18 GMT
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
James Westby - 10 Jan 2006 20:25 GMT
> Hello
>
[quoted text clipped - 10 lines]
>
> Thanks

Try

.equals('a'){

becomes

== 'a'){

James
Roedy Green - 10 Jan 2006 20:49 GMT
>> 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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

VisionSet - 10 Jan 2006 20:29 GMT
> Hello
>
[quoted text clipped - 5 lines]
>
> 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
haig - 10 Jan 2006 20:39 GMT
"VisionSet" <spam@ntlworld.com> 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)
[quoted text clipped - 14 lines]
>
>  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...
James Westby - 10 Jan 2006 20:51 GMT
> "VisionSet" <spam@ntlworld.com> wrote in news:WOUwf.32187$yu.5572
> @newsfe6-gui.ntli.net:
[quoted text clipped - 29 lines]
>
> 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 - 10 Jan 2006 21:09 GMT
>> "VisionSet" <spam@ntlworld.com> wrote in news:WOUwf.32187$yu.5572
>> @newsfe6-gui.ntli.net:
[quoted text clipped - 34 lines]
>
> James

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

James
Roedy Green - 10 Jan 2006 20:52 GMT
>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[].

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Mike Schilling - 11 Jan 2006 02:52 GMT
>>String [] vouwels = {"A", "a", "E", "e", "U", "u", "I", "i", "O", "o"};
>>
[quoted text clipped - 8 lines]
>
> But you need to use a char[] instead of a String[].

Simpler, though is:

   int index = "AaEeIiOoUu".indexOf (word.charAt(i));
   if (index >= 0) ...
Thomas Fritsch - 10 Jan 2006 20:58 GMT
> And if I want to compare a string of vouwels
>
[quoted text clipped - 3 lines]
>
> 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]){}

Signature

"TFritsch$t-online:de".replace(':','.').replace('$','@')

Thomas Hawtin - 10 Jan 2006 21:20 GMT
>>And if I want to compare a string of vouwels
>>
[quoted text clipped - 9 lines]
> 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
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Malte Christensen - 10 Jan 2006 20:54 GMT
> Hello
>
[quoted text clipped - 10 lines]
>
> Thanks

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

Try

if (word.charAt(i) == 'a') {
...
}
Roedy Green - 10 Jan 2006 21:30 GMT
On Tue, 10 Jan 2006 21:54:09 +0100, Malte Christensen
<You_can_spam_me_here@nmalte.dk> 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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Malte Christensen - 11 Jan 2006 15:03 GMT
> On Tue, 10 Jan 2006 21:54:09 +0100, Malte Christensen
> <You_can_spam_me_here@nmalte.dk> wrote, quoted or indirectly quoted
[quoted text clipped - 5 lines]
> 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.
java_programmer - 11 Jan 2006 18:15 GMT
It sounds like you may even want to look at regular expressions to do
your test...
Roedy Green - 10 Jan 2006 20:57 GMT
>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.  
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.