char cannot be dereferenced

H

haig

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
 
J

James Westby

haig said:
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
 
V

VisionSet

haig said:
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' ) {...} // !!
 
H

haig

@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...
 
R

Roedy Green

You have two problems. equals is for comparing objects; == is for
comparing primitives. You have primitives. Secondly your () don't
balance.
 
J

James Westby

haig said:
@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
 
R

Roedy Green

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[].
 
M

Malte Christensen

haig said:
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') {
....
}
 
T

Thomas Fritsch

haig said:
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]){}
 
J

James Westby

James said:
haig said:
@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
 
T

Thomas Hawtin

Thomas said:
haig said:
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
 
R

Roedy Green

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

Mike Schilling

Roedy Green 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) ...
 
M

Malte Christensen

Roedy said:
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.
 
J

java_programmer

It sounds like you may even want to look at regular expressions to do
your test...
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top