Timdoozy
Timdoozy

Reputation: 21

Missing return statement when using .charAt

I need to write a code that returns the number of vowels in a word, I keep getting an error in my code asking for a missing return statement. Any solutions please? :3

import java.util.*;

public class vowels
{
public static void main(String[] args)
{
    Scanner input = new Scanner(System.in);
    System.out.println("Please type your name.");
    String name = input.nextLine();
    System.out.println("Congratulations, your name has "+
                        countVowels(name) +" vowels.");
}
public static int countVowels(String str)
{
    int count = 0;
    for (int i=0; i < str.length(); i++)
    {
        // char c = str.charAt(i);
        if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'o' || str.charAt(i) == 'i' || str.charAt(i) == 'u')
        count = count + 1;
    }
}
}

Upvotes: 0

Views: 238

Answers (1)

user1345223
user1345223

Reputation:

As several comments point out, you're missing a return statement.

You need to return count.

public static int countVowels(String str)
{
    int count = 0;
    for (int i=0; i < str.length(); i++)
    {
        // char c = str.charAt(i);
        if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'o' ||  
            str.charAt(i) == 'i' || str.charAt(i) == 'u')
        count = count + 1;
    }

    return count;
}

Upvotes: 2

Related Questions