Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Vowel frequency in a string, it compiles but getting error

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Vowel frequency in a string, it compiles but getting error

    import java.util.Scanner;
    public class program4f_4
    {
        public static void main(String args[])
        {
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter a string to see how many times each vowel occurs.");
            String message = sc.nextLine();
            message = message.toLowerCase();
            int length = message.length();
            vowelFrequency(message, length);
     
        }
     
        static public void vowelFrequency(String message, int length)
        {
            int vowelA=0;
            int vowelE=0;
            int vowelI=0;
            int vowelO=0;
            int vowelU=0;
            int x = 0;
            char vowel;
     
            for(x = 0; x <= length; x++)
            {
     
                vowel = message.charAt(x);
                if (vowel == 'a')
                    vowelA++;
                if (vowel == 'e')
                    vowelE++;
                if (vowel == 'i')
                    vowelI++;
                if (vowel == 'o')
                    vowelO++;
                if (vowel == 'u')
                    vowelU++;
     
            }
            System.out.println("Vowel       Occurances");
            System.out.println("A:    " + vowelA);
            System.out.println("E:    " + vowelE);
            System.out.println("I:    " + vowelI);
            System.out.println("O:    " + vowelO);
            System.out.println("U:    " + vowelU);
     
        }
     
    }

    When I run it it highlights "vowel = message.charAt(x);" and the error message is
    "java.lang.StringIndexOutofBoundsException:
    String index out of range: 10 (in java.lang.String)". I'm thinking it has something to do with char variable type?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Vowel frequency in a string, it compiles but getting error

    "java.lang.StringIndexOutofBoundsException: String index out of range: 10 (
    The index value went past the end of the String.
    Remember that index values range from 0 to the String length-1. If the String has 10 characters, the max index is 9.

    In the for loop, stop the loop BEFORE the index gets to the String's length;
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    lordofrandom (March 29th, 2013)

  4. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Vowel frequency in a string, it compiles but getting error

    Thanks a lot.

Similar Threads

  1. Creating a Simple, Beginner-Level Vowel Counter
    By tryingtolearn in forum Collections and Generics
    Replies: 1
    Last Post: July 8th, 2012, 03:25 PM
  2. get vowel character from strings
    By siddiqui_1985 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 2nd, 2011, 04:30 AM
  3. Do While loop + switch, Vowel Count
    By mwardjava92 in forum Loops & Control Statements
    Replies: 3
    Last Post: November 9th, 2011, 11:46 AM
  4. Finding frequency and probability of characters in a string
    By Aberforth in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 31st, 2010, 02:02 AM
  5. Replies: 2
    Last Post: November 3rd, 2009, 06:28 AM