Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

char cannot be dereferenced

 
 
matt
Guest
Posts: n/a
 
      08-09-2005
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

------------------------

Any ideas?

 
Reply With Quote
 
 
 
 
Joan
Guest
Posts: n/a
 
      08-09-2005

"matt" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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.

 
Reply With Quote
 
 
 
 
Timbo
Guest
Posts: n/a
 
      08-09-2005
matt wrote:
> 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
>
> ------------------------
>
> Any ideas?
>

'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.
 
Reply With Quote
 
matt
Guest
Posts: n/a
 
      08-09-2005
That was it, thank you.

-matt

 
Reply With Quote
 
matt
Guest
Posts: n/a
 
      08-09-2005
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);

}
}

 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      08-09-2005
matt wrote:
> 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
 
Reply With Quote
 
matt
Guest
Posts: n/a
 
      08-09-2005
It only returns the first occurance of the 'e'.

 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      08-09-2005
matt wrote:
> 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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      08-09-2005
On 9 Aug 2005 08:36:48 -0700, "matt" <(E-Mail Removed)> wrote or
quoted :

>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/...s_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      08-10-2005
On 9 Aug 2005 08:36:48 -0700, "matt" <(E-Mail Removed)> wrote or
quoted :

>C:\JAVAWO~1\Chapter.05>javac VowelCount.java
>VowelCount.java:9: char cannot be dereferenced


see
http://mindprod.com/jgloss/compileer...BEDEREFERENCED
--
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/...s_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
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 haig Java 15 01-11-2006 06:15 PM
Pointer: why could a deleted pointer be dereferenced? B. Penn C++ 6 08-09-2004 10:27 PM



Advertisments