Java Program to Count the Number of Vowels in a String

Ramesh Fadatare
3 min readDec 13, 2024

--

Introduction

Counting the number of vowels in a string is a common task in text processing. In Java, this exercise helps you understand how to manipulate strings and use loops or regular expressions. This guide will walk you through writing a Java program that counts the number of vowels in a given string.

Learn everything about Java: https://www.javaguides.net/

Problem Statement

Create a Java program that:

  • Prompts the user to enter a string.
  • Counts the number of vowels (a, e, i, o, u) in the string, regardless of case.
  • Displays the count of vowels.

Example:

  • Input: "Hello World"
  • Output: Number of vowels: 3

Solution Steps

  1. Read the String: Use the Scanner class to take the string as input from the user.
  2. Initialize a Vowel Counter: Create a variable to store the count of vowels.
  3. Iterate Through the String: Loop through each character in the string and check if it is a vowel.
  4. Count the Vowels: Increment the counter each time a vowel is found.
  5. Display the Vowel Count: Print the total count of vowels.
  6. Close Resources: Close the Scanner class object automatically using the try-resource statement.

Java Program

// Java Program to Count the Number of Vowels in a String
// Author: https://www.rameshfadatare.com/

import java.util.Scanner;

public class VowelCounter {
public static void main(String[] args) {
// Step 1: Read the string from the user
try(Scanner scanner = new Scanner(System.in)){
System.out.print("Enter a string: ");
String input = scanner.nextLine();

// Step 2: Initialize the vowel counter
int vowelCount = 0;

// Step 3: Iterate through the string
for (int i = 0; i < input.length(); i++) {
char ch = input.charAt(i);

// Step 4: Check if the character is a vowel
if (isVowel(ch)) {
vowelCount++;
}
}

// Step 5: Display the vowel count
System.out.println("Number of vowels: " + vowelCount);
}
}

// Helper method to check if a character is a vowel
public static boolean isVowel(char ch) {
ch = Character.toLowerCase(ch);
return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u';
}
}

Explanation

Step 1: Read the String

  • The Scanner class is used to read a string input from the user. The nextLine() method captures the entire line as a string.

Step 2: Initialize the Vowel Counter

  • A simple integer variable vowelCount is initialized to zero. This will hold the count of vowels found in the string.

Step 3: Iterate Through the String

  • A for loop is used to iterate over each character in the string, using the charAt() method to access individual characters.

Step 4: Check if the Character is a Vowel

  • The isVowel() method is a helper function that checks if a given character is a vowel. It converts the character to lowercase to handle case-insensitivity.

Step 5: Display the Vowel Count

  • The program prints the total number of vowels found in the string using System.out.println().

Output

Enter a string: Hello World
Number of vowels: 3

Conclusion

This Java program demonstrates how to count the number of vowels in a user-input string. It covers essential concepts such as string manipulation, loops, and conditionals, making it a valuable exercise for beginners learning Java programming.

Learn everything about Java: https://www.javaguides.net/

--

--

No responses yet