tysco
tysco

Reputation: 25

Number of vowels within an array

I'm trying to write a program which declares and initializes an array of characters (char[] word) and calls the method:

public static int countVowels(char[])

which returns the number of vowels in word.

Can anyone tell me where im going wrong? Getting this error

java:11: error: char cannot be dereferenced
for (int j=0; j < word[i].length(); j++) { 
                         ^
array.java:12: error: char cannot be dereferenced
char c = word[i].charAt(j); 
                ^
2 errors

 

public class array { 
 public static void main(String[] args) { 
  char[] word = {'a','b','f','u','g','i','o','r'};

}
public static int countVowels(char[] word) {
int vowelCount = 0; 

for (int i = 0; i < word.length; i++) 
{ 
    for (int j=0; j < word[i].length(); j++) { 
    char c = word[i].charAt(j); 
    if ( (c == 'a') 
    || (c == 'e') 
    || (c == 'i') 
    || (c == 'o') 
    || (c == 'u') 
    || (c == 'A') 
    || (c == 'E') 
    || (c == 'I') 
    || (c == 'O') 
    || (c == 'U') 
    ) 
    vowelCount++; 
   } 
  } 
 } 
}

Upvotes: 1

Views: 2617

Answers (4)

Levent Divilioglu
Levent Divilioglu

Reputation: 11602

You have more than one error;

Just use these;

for (int j=0; j < word.length; j++) { 
char c = word[i];

instead of these down below (these are invalid);

for (int j=0; j < word[i].length(); j++) { 
char c = word[i].charAt(j);

To get the length of the array, length is enough, you don't have to put the paranthesis. And also when you assign the ith char element of the word array, c = word[i]; is valid.

I've just corrected your method and just have written a test code for it. You also used unnecessary outer for-loop, which causes invalid result.

Additional to your corrected method, I've added my method for counting vowels, which uses a secondary array for storing the vowels. It's much more readable but the choice is yours ;)

public class TestCountVowels {

    public static void main(String[] args) {
        char[] word = {'a','b','f','u','g','i','o','r'};

        //I advice calling geVowelCount(char[]) method
        System.out.println("Vowel count: " + getVowelCount(word) );

        //Calling your method with my corrections
        System.out.println("Vowel count: " + yourCountMethod(word) );


    }

    //My method for comparing char arrays and counting
    public static int getVowelCount(char[] inputWord) {
        char[] vowels = {'a','A','e','E','i','I','o','O','u','U'};
        int vowelCount = 0;

        for( int i = 0; i < inputWord.length; i++ ) {
            for( int j = 0; j < vowels.length; j++ )
                if ( inputWord[i] == vowels[j] ) {
                    vowelCount++;
                    continue;
                }
        }

        return vowelCount;
    }

    //Your method with corrections
    public static int yourCountMethod(char[] word) {
        int vowelCount = 0; 

        for (int i = 0; i < word.length; i++) 
        {
            char c = word[i];
            if (
                    (c == 'a') || 
                    (c == 'e') || 
                    (c == 'i') || 
                    (c == 'o') || 
                    (c == 'u') || 
                    (c == 'A') || 
                    (c == 'E') || 
                    (c == 'I') || 
                    (c == 'O') || 
                    (c == 'U') 
                ) 
                vowelCount++; 
        }

        return vowelCount;
    }

}

Hope that it helps.

Upvotes: 0

alexgbelov
alexgbelov

Reputation: 3121

When you call word[i], you're getting the value stored at the ith position in the array word. So, word[i].length returns the length of the value stored in the ith position. You're getting an error because the value stored is a char, which doesn't have a length attribute. Instead, try just word.length. This will give you the length of the array.

With this information, you should have enough to fix your for loop code. Remember, word[i] returns a char.

Upvotes: 1

Balkrishna Rawool
Balkrishna Rawool

Reputation: 1863

You don't really need the inner loop. Try this:

public class array { 
 public static void main(String[] args) { 
  char[] word = {'a','b','f','u','g','i','o','r'};

}
public static int countVowels(char[] word) {
int vowelCount = 0; 

for (int i = 0; i < word.length; i++) 
{ 
char c = word[i]; 
if ( (c == 'a') 
|| (c == 'e') 
|| (c == 'i') 
|| (c == 'o') 
|| (c == 'u') 
|| (c == 'A') 
|| (c == 'E') 
|| (c == 'I') 
|| (c == 'O') 
|| (c == 'U') 
) 
vowelCount++; 
  }
return vowelCount;
  } 
}

Upvotes: 2

Nina Scholz
Nina Scholz

Reputation: 386560

Try word.length. The code should not run in the browser, because it's not javascript. instead it is java.

Upvotes: 0

Related Questions