Results 1 to 9 of 9
  1. #1
    gabrielpr12 is offline Member
    Join Date
    Nov 2011
    Posts
    41
    Rep Power
    0

    Default Counting Vowels, getting an error

    I am trying to create a program that counts the number of vowels in an array. I keep getting the following error:
    Vowels.java:12: error: cannot find symbol
    c = name.charAt(i);
    ^
    symbol: method charAt(int)
    location: variable name of type String[]
    1 error


    Here is my code:


    import java.util.*;
    import javax.swing.*;

    public class Vowels
    {
    public static void main(String[] args)
    {
    String[] name = {"Mariel", "Yadrianna", "Zeus", "Gabriel", "Luz"};
    char c;
    int i;
    int count = 0;
    for(i = 0; i < name.length; i++)
    {
    c = name.charAt(i);

    if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u')
    {
    count++;
    }
    }

    }
    }

    I know I still have to print or display the actual number of vowels.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default Re: Counting Vowels, getting an error

    name = string array.an array has no method charAt.
    you have to iterate over each element in the array(each name) and then you have to iterate over each character of this string (i think that was what you want - there are of course other solutions)

    pseudo:
    for(0...array.length)
    name = array[i];
    for(0 ... name.length)
    character = name.charAt(j)
    .....

  3. #3
    gabrielpr12 is offline Member
    Join Date
    Nov 2011
    Posts
    41
    Rep Power
    0

    Default Re: Counting Vowels, getting an error

    Quote Originally Posted by eRaaaa View Post
    name = string array.an array has no method charAt.
    you have to iterate over each element in the array(each name) and then you have to iterate over each character of this string (i think that was what you want - there are of course other solutions)

    pseudo:
    for(0...array.length)
    name = array[i];
    for(0 ... name.length)
    character = name.charAt(j)
    .....
    Not understanding what you are trying to say. I have used the charAt in other applications with no errors. Why is it giving me an error now?

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default Re: Counting Vowels, getting an error

    Because you are trying to invoke the charAt method on an array object (String[] name) and not from a string!
    name[0].charAt would be valid!

  5. #5
    gabrielpr12 is offline Member
    Join Date
    Nov 2011
    Posts
    41
    Rep Power
    0

    Default Re: Counting Vowels, getting an error

    Quote Originally Posted by eRaaaa View Post
    Because you are trying to invoke the charAt method on an array object (String[] name) and not from a string!
    name[0].charAt would be valid!
    Thank you, that fixed that. Now, if I want to count the vowels on each array, would I have to do a if loop for each one?

  6. #6
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default Re: Counting Vowels, getting an error

    Thats what i said in my first post :D
    But there are other solutions, like
    Java Code:
    		for (String string : name) {
    			System.out.println("Vowel count of "+string+" is "+string.replaceAll("[^aeiou]", "").length());
    		}
    :D

  7. #7
    gabrielpr12 is offline Member
    Join Date
    Nov 2011
    Posts
    41
    Rep Power
    0

    Default Re: Counting Vowels, getting an error

    Sorry for my noobinesh. I will keep working on this and post the final product once I figure it out.

  8. #8
    gabrielpr12 is offline Member
    Join Date
    Nov 2011
    Posts
    41
    Rep Power
    0

    Default Re: Counting Vowels, getting an error

    Thanks for the help. Here is the final working code:
    import java.util.*;
    import javax.swing.*;

    public class Vowels
    {
    public static void main(String[] args)
    {
    String[] name = {"Mariel", "Yadrianna", "Zeus", "Gabriel", "Luz"};

    int i;
    int count = 0;

    for(String string: name)
    {
    System.out.println("Vowel count of "+string+" is "+string.replaceAll("[^aeiou]", "").length());
    }
    for(i = 0; i < name.length; i++)
    {
    char c = name[0].charAt(i);

    if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u' ||
    c=='A' || c=='E' || c=='I' || c=='O' || c=='U')
    {
    count++;

    }
    }
    }
    }

  9. #9
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default Re: Counting Vowels, getting an error

    ouch....you should not just copy code. Try to understand what the code does. Your code(second loop) makes no sense :-)
    Remove my solution and start from cratch!

Similar Threads

  1. Replies: 4
    Last Post: 08-12-2011, 05:47 PM
  2. counting the vowels in a sentence
    By huntrguy102 in forum New To Java
    Replies: 3
    Last Post: 11-08-2010, 06:31 AM
  3. Replacing Vowels in a word.
    By mklprasad in forum Advanced Java
    Replies: 1
    Last Post: 10-05-2009, 12:31 PM
  4. Code counting error
    By Adde1986 in forum New To Java
    Replies: 2
    Last Post: 03-18-2009, 01:02 AM
  5. Counting Vowels and Constonants
    By MattN in forum New To Java
    Replies: 3
    Last Post: 11-20-2007, 05:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •