-2

I am creating a program that counts the number of vowels, consonants, spaces, and punctuation in a string. Before moving on to anything else, I'm making sure that the vowel checker works. I've created a loop that seems to work logically, as I've run it through a java visualizer, and everything checks out. But, I get an error when I run it through the BlueJ IDE. It says: java.lang.StringIndexOutOfBoundsException: String index out of range. I have no idea what the issue is, and I would appreciate any and all help. This is a link to the java visualizer if anyone wants to test it for themselves, and I have the code posted below:

//**************************************************************
 // Testing program for VCP program (temporary).
 //
 // @aaron_ford
 // @version_1.0_11.9.17
 //**************************************************************
public class VCPTest
{
    public static void main(String[] args)
    {
        System.out.println("Enter a string.");
        String user_str = "a vowel is here";
        System.out.println("You entered: " + user_str);

        char vowels[] = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};

        // counter variables
        int v_counter = 0;
        int c_counter = 0;
        int s_counter = 0;
        int p_counter = 0;
        int count = 0;
        int str_compare = user_str.charAt(count);

        for (; count < str_compare; count++)
        {
            str_compare = user_str.charAt(count);
            for (int a:vowels)
            {
                if (a == str_compare)
                {
                    v_counter++;
                }
            }
        }

        System.out.println("There are " + v_counter + " vowels.");
        System.out.println("There are " + c_counter + " consonants.");
        System.out.println("There are " + s_counter + " spaces.");
        System.out.println("There are " + p_counter + " punctuation marks.");
    }     
}
Alphox
  • 1
  • 4

1 Answers1

0

The problem is with your "for" loop. You check if count value is less than str_compare value (which is a numeric representation of character from given string). I think this is not what you were expecting. I would suggest few little modifications - it provides checking character from given string ( str_compare = user_str.charAt(count); ) and after every loop count value is being increased to check next character from this string. In every loop there is validation made, to check whether the end of string is currently reached (count < user_str.length())

for (count = 0; count < user_str.length(); count++)
        {
            str_compare = user_str.charAt(count);
            for (int a : vowels)
            {
                if (a == str_compare)
                {
                    v_counter++;
                }
            }
        }

And now the output of your program looks better:

Enter a string.
You entered: a vowel is here
There are 6 vowels.
There are 0 consonants.
There are 0 spaces.
Przemysław Moskal
  • 3,551
  • 2
  • 12
  • 21