char cannot be dereferenced

M

matt

Here is my code:

public class VowelCount{
public static void main(String[] args) {
String[] firstNames =
{"Matthew","Anne","Gerard","Gerard","Jennifer"};
char[] vowels = {'a','e','i','o','u','A','E','I','O','U'};
int count = 0;

for(int x = 0; x < vowels.length; ++x){
for(int y = 0; y < firstNames.length; ++y){
if(vowels[x].indexOf(firstNames[y]) > 0){
count += 1;
}
}
}
System.out.println(count);

}
}
-----------------------
Here is the compile error:

C:\JAVAWO~1\Chapter.05>javac VowelCount.java
VowelCount.java:9: char cannot be dereferenced
if(vowels[x].indexOf(firstNames[y]) > 0){
^
1 error
 
J

Joan

matt said:
Here is my code:

public class VowelCount{
public static void main(String[] args) {
String[] firstNames =
{"Matthew","Anne","Gerard","Gerard","Jennifer"};
char[] vowels = {'a','e','i','o','u','A','E','I','O','U'};
int count = 0;

for(int x = 0; x < vowels.length; ++x){
for(int y = 0; y < firstNames.length; ++y){
if(vowels[x].indexOf(firstNames[y]) > 0){
count += 1;
}
}
}
System.out.println(count);

}
}
-----------------------
Here is the compile error:

C:\JAVAWO~1\Chapter.05>javac VowelCount.java
VowelCount.java:9: char cannot be dereferenced
if(vowels[x].indexOf(firstNames[y]) > 0){
^
1 error
It looks like you are trying to locate a string inside a char
rather than a char inside a string.
 
T

Timbo

matt said:
Here is my code:

public class VowelCount{
public static void main(String[] args) {
String[] firstNames =
{"Matthew","Anne","Gerard","Gerard","Jennifer"};
char[] vowels = {'a','e','i','o','u','A','E','I','O','U'};
int count = 0;

for(int x = 0; x < vowels.length; ++x){
for(int y = 0; y < firstNames.length; ++y){
if(vowels[x].indexOf(firstNames[y]) > 0){
count += 1;
}
}
}
System.out.println(count);

}
}
-----------------------
Here is the compile error:

C:\JAVAWO~1\Chapter.05>javac VowelCount.java
VowelCount.java:9: char cannot be dereferenced
if(vowels[x].indexOf(firstNames[y]) > 0){
^
1 error
'char' is a primitive java type and can therefore not be
dereferenced like an object. From your code snippet, I suspect you
mean to have: "if (firstNames[y].indexOf(vowels[x]) > 0)" anyway,
because "indexOf" does not really make sense on a character.
 
M

matt

Ok now that I have changed the code it is not counting correctly it is
counting 3 vowels instead of the 4 it should be.

public class VowelCount{
public static void main(String[] args) {
String[] firstNames = {"Come","here"};
char[] vowels = {'a','e','i','o','u','A','E','I','O','U'};
int count = 0;

for(int x = 0; x < vowels.length; ++x){
for(int y = 0; y < firstNames.length; ++y){
if(firstNames[y].indexOf(vowels[x]) > -1){
count += 1;
}
}
}
System.out.println(count);

}
}
 
P

Patricia Shanahan

matt said:
Ok now that I have changed the code it is not counting correctly it is
counting 3 vowels instead of the 4 it should be.

public class VowelCount{
public static void main(String[] args) {
String[] firstNames = {"Come","here"};
char[] vowels = {'a','e','i','o','u','A','E','I','O','U'};
int count = 0;

for(int x = 0; x < vowels.length; ++x){
for(int y = 0; y < firstNames.length; ++y){
if(firstNames[y].indexOf(vowels[x]) > -1){
count += 1;
}
}
}
System.out.println(count);

}
}

Try stepping through what happens when firstnames[y] is "here" and
vowels[x] is 'e'.

Patricia
 
P

Patricia Shanahan

matt said:
It only returns the first occurance of the 'e'.

Correct. Your existing code counts the number of combinations of vowel
and name for which the vowel appears at least once in the name.

Is that the question you meant to ask? If not, you need to try to
restate your real question in Java.

Patricia
 
R

Roedy Green

C:\JAVAWO~1\Chapter.05>javac VowelCount.java
VowelCount.java:9: char cannot be dereferenced
if(vowels[x].indexOf(firstNames[y]) > 0){

That's a peculiar error message but what is means is indexOf is a
method of String not char[].


--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
R

Roedy Green

G

Grant Wagner

matt said:
Ok now that I have changed the code it is not counting correctly it is
counting 3 vowels instead of the 4 it should be.

public class VowelCount{
public static void main(String[] args) {
String[] firstNames = {"Come","here"};
char[] vowels = {'a','e','i','o','u','A','E','I','O','U'};
int count = 0;

for(int x = 0; x < vowels.length; ++x){
for(int y = 0; y < firstNames.length; ++y){
if(firstNames[y].indexOf(vowels[x]) > -1){
count += 1;
}
}
}
System.out.println(count);

}
}

I think that it's pretty obvious why.

When you count the vowels in the word "here", it unravels as follows:

if ("here".indexOf('a') > 0) { ++count; }
if ("here".indexOf('e') > 0) { ++count; }
if ("here".indexOf('i') > 0) { ++count; }
if ("here".indexOf('o') > 0) { ++count; }
// ...etc

Now you can see that you aren't counting the number of vowels in a word,
you are counting _the first occurrance of a vowel in the words_. If the
same vowel appears more than once you take no notice of it because you
count it once and then move on to the next vowel.

For each vowel for each word, you need to test word.indexOf(vowel,
positionOfLastVowel + 1) until it returns -1. There are a variety of
other ways to do this, but you've started with indexOf() and so I
provided you with a suggestion that uses your existing design.
 

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,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top