3

This program I'm making for a COSC course isn't compiling right, I keep getting the error:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 2

at java.lang.String.substring(String.java:1765) at VowelCount.main(VowelCount.java:13)

Here's my code:

import java.util.Scanner;

public class VowelCount {
 public static void main(String[] args) {
  int a = 0, e = 0, i = 0, o = 0, u = 0, count = 0;
  String input, letter;
  Scanner scan = new Scanner (System.in);

  System.out.println ("Please enter a string: ");
  input = scan.nextLine();

  while (count <= input.length() ) {
   letter = input.substring(count, (count + 1));

   if (letter == "a") {
    a++; }
   if (letter == "e") {
    e++; }
   if (letter == "i") {
    i++; }
   if (letter == "o") {
    o++; }
   if (letter == "u") {
    u++; }

   count++;

  }
  System.out.println ("There are " + a + " a's.");
  System.out.println ("There are " + e + " e's.");
  System.out.println ("There are " + i + " i's.");
  System.out.println ("There are " + o + " o's.");
  System.out.println ("There are " + u + " u's.");
 }
}

To my knowledge this should work, but why doesn't it? Any help would be great. Thank you!

5 Answers 5

5

You may need to take out the = in the line

while (count <= input.length() ) {

and make it

while (count < input.length() ) {

because it is causing the substring to read beyond the length of the string.

=============== But I'll add a few extra bits of advice even though its not asked for:

do not use == to compare strings, use

letter.equals("a")

instead. Or even better, try using

char c = input.charAt(count);

to get the current character then compare like this:

c == 'a'
Sign up to request clarification or add additional context in comments.

Comments

0

I think your loop condition should be count < input.length. Right now, the last iteration runs with count == length, so your substring call is given a start index after the last character in the string, which is illegal. These type of boundary errors are very common when writing such loops, so it's always good to double- and triple-check your loop conditions when you encounter a bug like this.

Also, comparing strings with the == operator usually won't do what you want. That compares whether or not the two variables reference the same object. Instead, you want to test string1.equals(string2), which compares the contents of the two strings.

Comments

0

Removing the equal sign should fix that.

while (count < input.length()) {

and since you want to get a single character, you should do this:

substr(count,1)

because the 2nd parameter is actually length, not index.

2 Comments

Alright, it compiles! But still doesn't output the right amounts. Test string "aeiou" results in 0, 0, 0, 0, 0..
Do not use == to compare strings as mentioned in other answers
0

Fixed it with help from everyone, and especially Vincent. Thank you! Runs wonderfully.

import java.util.Scanner;

public class VowelCount {
    public static void main(String[] args) {
        int a = 0, e = 0, i = 0, o = 0, u = 0, count = 0;
        String input;
        char letter;

        Scanner scan = new Scanner (System.in);

        System.out.print ("Please enter a string: ");
        input = scan.nextLine();

        while (count < input.length() ) {
            letter = input.charAt (count);

            if (letter == 'a')
                a++; 
            if (letter == 'e') 
                e++; 
            if (letter == 'i') 
                i++; 
            if (letter == 'o') 
                o++; 
            if (letter == 'u') 
                u++; 

            count++;

        }
        System.out.println ("There are " + a + " a's.");
        System.out.println ("There are " + e + " e's.");
        System.out.println ("There are " + i + " i's.");
        System.out.println ("There are " + o + " o's.");
        System.out.println ("There are " + u + " u's.");
    }
}

4 Comments

you can use a for loop to tidy it up even further: - for(int count = 0; count < input.length(); count++)
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post.
@WilQu did you read this question? it's the OP posting the working solution. how should this be a comment?
@meda I see this as a comment only saying that the other answers worked. It does not add anything to the existing answers.
0

Before loop,try below

if(input.length()>0){
//you code
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.