Results 1 to 5 of 5

Thread: vowel couting

  1. #1
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    5

    Default vowel couting

    hello, i found this problem on the net and i am trying to solve it but im not getting the required output

    the problem is:

    Write a static method named vowelCount that accepts a String as a parameter and produces and returns an array of integers representing the counts of each vowel in the String. The array returned by your method should hold 5 elements: the first is the count of As, the second is the count of Es, the third Is, the fourth Os, and the fifth Us. Assume that the string contains no uppercase letters.

    For example, the call vowelCount("i think, therefore i am") should return the array {1, 3, 3, 1, 0}.
    i did a code, but its not working here is my code

    Java Code:
    public static int [] vowelCount (String str)
        {
            int [] vowel = new int [5];
       
            for ( int i = 0; i < str.length(); i++)
            {
                if ( str.substring(i) == "a" )
                {
                    vowel[0]++;
                }else if ( str.substring(i) == "e" ){
                    vowel[1]++;
                }else if ( str.substring(i) == "i" ){
                    vowel[2]++;
                }else if ( str.substring(i) == "o" ){
                    vowel[3]++;
                }else if ( str.substring(i) == "u" ){
                    vowel[4]++;
                }
            }
       
            return vowel;
        }
    can anyone help me plz

    EDIT: I figured it out, no need
    Last edited by aizen92; 01-12-2011 at 01:48 PM.

  2. #2
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    Quote Originally Posted by aizen92 View Post
    EDIT: I figured it out, no need
    Glad to know that you figured it out on your own.

    I believe, you must have used

    Java Code:
     if (str.charAt(i) == 'a')
    instead of
    Java Code:
    if (str.substring(i) == "a" )

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  3. #3
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    5

    Default

    ahaa, i didnt think of the chatAt i forgot it

    anyway it seemed that the way i though of didnt work, cuz i though ot it but didnt try it yet, but a while ago i tried it, but didnt work, so i resorted to the charAt

    my though was to put each letter in an array and compare each element of the array, but for some reason it didnt work

    anyway, thnx for the help goldest :D

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I know you solved it, and I'm wondering if it was that you had substring(i) which creates a substring from i to end of string.

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Also, please se the topic to solved if you have no more questions in this regard. Thanks and good luck!

Similar Threads

  1. total number of words starting with a vowel
    By somnath6088 in forum New To Java
    Replies: 4
    Last Post: 11-08-2010, 10:52 AM

Posting Permissions

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