char cannot be dereferenced

Discussion in 'Java' started by matt, Aug 9, 2005.

  1. matt

    matt Guest

    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?
     
    matt, Aug 9, 2005
    #1
    1. Advertising

  2. matt

    Joan Guest

    "matt" <> wrote in message
    news:...
    > 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.
     
    Joan, Aug 9, 2005
    #2
    1. Advertising

  3. matt

    Timbo Guest

    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.
     
    Timbo, Aug 9, 2005
    #3
  4. matt

    matt Guest

    That was it, thank you.

    -matt
     
    matt, Aug 9, 2005
    #4
  5. matt

    matt Guest

    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);

    }
    }
     
    matt, Aug 9, 2005
    #5
  6. 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
     
    Patricia Shanahan, Aug 9, 2005
    #6
  7. matt

    matt Guest

    It only returns the first occurance of the 'e'.
     
    matt, Aug 9, 2005
    #7
  8. 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
     
    Patricia Shanahan, Aug 9, 2005
    #8
  9. matt

    Roedy Green Guest

    On 9 Aug 2005 08:36:48 -0700, "matt" <> 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/mckinney_grills_rumsfeld.htm

    Canadian Mind Products, Roedy Green.
    See http://mindprod.com/iraq.html photos of Bush's war crimes
     
    Roedy Green, Aug 9, 2005
    #9
  10. matt

    Roedy Green Guest

    On 9 Aug 2005 08:36:48 -0700, "matt" <> wrote or
    quoted :

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


    see
    http://mindprod.com/jgloss/compileerrormessages.html#CHARCANNOTBEDEREFERENCED
    --
    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
     
    Roedy Green, Aug 10, 2005
    #10
  11. matt

    Grant Wagner Guest

    "matt" <> wrote in message
    news:...
    > 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.

    --
    Grant Wagner <>
     
    Grant Wagner, Sep 2, 2005
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. haig
    Replies:
    15
    Views:
    27,139
    java_programmer
    Jan 11, 2006
  2. B. Penn
    Replies:
    6
    Views:
    616
    Old Wolf
    Aug 9, 2004
  3. lovecreatesbeauty
    Replies:
    1
    Views:
    1,132
    Ian Collins
    May 9, 2006
  4. Replies:
    7
    Views:
    982
    Bo Persson
    Oct 30, 2006
  5. justineee
    Replies:
    1
    Views:
    1,094
    Mark Space
    Oct 24, 2008
Loading...

Share This Page