Article Body
When working with strings in Java, one common beginner-level task is to count the number of vowels and consonants in a given string. This simple program helps you understand loops, conditions, and character checking in Java.
Letβs break down the problem and build the solution step-by-step in plain English.
π What is a Vowel and a Consonant?
In the English alphabet:
-
Vowels are:
A, E, I, O, U(both lowercase and uppercase) -
Consonants are all the other letters except vowels (like
B, C, D, F...)
We will ignore digits, spaces, and special characters in our count.
π οΈ Problem Statement
Input: A string from the user (e.g., βHello Worldβ)
Output: Count of vowels and consonants in the string
β Java Code to Count Vowels and Consonants
import java.util.Scanner;
public class VowelConsonantCounter {
public static void main(String[] args) {
// Step 1: Create Scanner object to take input from user
Scanner scanner = new Scanner(System.in);
// Step 2: Ask the user to enter a string
System.out.print("Enter a string: ");
String input = scanner.nextLine();
// Step 3: Initialize counters for vowels and consonants
int vowelCount = 0;
int consonantCount = 0;
// Step 4: Convert input to lowercase to simplify comparison
input = input.toLowerCase();
// Step 5: Loop through each character in the string
for (int i = 0; i < input.length(); i++) {
char ch = input.charAt(i);
// Step 6: Check if the character is a letter
if ((ch >= 'a' && ch <= 'z')) {
// Step 7: Check if it's a vowel
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
vowelCount++;
} else {
consonantCount++;
}
}
// Characters that are not letters are ignored
}
// Step 8: Print the result
System.out.println("Number of vowels: " + vowelCount);
System.out.println("Number of consonants: " + consonantCount);
// Step 9: Close the scanner
scanner.close();
}
}
π§ Step-by-Step Explanation
β Step 1 β Taking Input
We use a Scanner to take input from the user.
Scanner scanner = new Scanner(System.in);
β Step 2 β Ask for a String
We prompt the user to enter a line of text.
System.out.print("Enter a string: ");
String input = scanner.nextLine();
β Step 3 β Initialize Counters
We define two variables:
-
vowelCountfor vowels -
consonantCountfor consonantsint vowelCount = 0; int consonantCount = 0;β Step 4 β Convert to Lowercase
To make checking easier, we convert the input to lowercase.
input = input.toLowerCase();This way, we donβt have to check both
Aanda, orEande, etc.
β Step 5 β Loop Through the String
We loop over every character using a
forloop.for (int i = 0; i < input.length(); i++) { char ch = input.charAt(i);β Step 6 β Check if It's a Letter
We check if the character is between
atoz. This filters out numbers, spaces, and symbols.if ((ch >= 'a' && ch <= 'z')) {β Step 7 β Check for Vowels
If itβs a vowel (from the list), we increase the vowel counter.
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { vowelCount++; } else { consonantCount++; }If not a vowel, it must be a consonant β so we increase
consonantCount.
β Step 8 β Print Results
After the loop, we display the total count of vowels and consonants.
System.out.println("Number of vowels: " + vowelCount); System.out.println("Number of consonants: " + consonantCount);β Step 9 β Close the Scanner
Itβs a good practice to close the scanner to prevent resource leaks.
scanner.close();π Example Run
Enter a string: Hello World Number of vowels: 3 Number of consonants: 7π― Why This Program is Important for Beginners
-
It helps you understand character checking
-
You practice loops and conditionals
-
You get comfortable with user input and string manipulation
π‘ Bonus Tip
If you want to count digits and special characters too, you can add more conditions using:
Character.isDigit(ch) Character.isWhitespace(ch)π§Ύ Conclusion
In this blog post, we learned how to count vowels and consonants in a string using Java. This small but powerful program teaches you key concepts like loops, conditions, and character handling, which are essential for Java programming.
Keep practicing with variations like counting uppercase vs lowercase letters, spaces, or digits, and youβll become better at logic building!
-

Comments