Java Program to Count Vowels and Consonants in a String (Java 8)

In this quick post, we will write a program that counts the number of vowels and consonants in a given string.
We will discuss two programs that count the number of vowels and consonants in a given string:
  1. String.charAt() solution
  2. Java 8, functional-style solution

Program 1: Java program that counts the number of vowels and consonants in a given string (using String.charAt())

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

//# Count vowels and consonants
public class CountVowelsAndConsonants {
 
    private static int vowels = 0;
    private static int consonants = 0;
    private static final String TEXT = "Java is Popular Programming Language";            
    
    public static void main(String[] args) {
        
        System.out.println("Input text: \n" + TEXT + "\n");
        
        System.out.println("String.charAt() solution:");
        
        countVowelsAndConsonants(TEXT);        
        
        System.out.println("Vowels: " + vowels);
        System.out.println("Consonants: " + consonants);
        
    }

    public static void countVowelsAndConsonants(String str) {

        if (str == null) {
            // or throw IllegalArgumentException
            throw new IllegalArgumentException("Input String can't be null");
        }

        str = str.toLowerCase();
        
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
                vowels++;
            } else if ((ch >= 'a' && ch <= 'z')) {
                consonants++;
            }
        }
    }
}
Output:
Input text: 
Java is Popular Programming Language

String.charAt() solution:
Vowels: 13
Consonants: 19

Program 2: Java program that counts the number of vowels and consonants in a given string (using Java 8, functional-style solution)

package net.javaguides.jdbc;

//# Count vowels and consonants
public class CountVowelsAndConsonants {

    private static long vowels = 0;
    private static long consonants = 0;
    private static final String TEXT = "Java is Popular Programming Language";

    public static void main(String[] args) {

        System.out.println("Input text: \n" + TEXT + "\n");

        System.out.println("Java 8, functional-style solution:");

        countVowelsAndConsonants(TEXT);

        System.out.println("Vowels: " + vowels);
        System.out.println("Consonants: " + consonants);

    }

    public static void countVowelsAndConsonants(String str) {

        if (str == null) {
            // or throw IllegalArgumentException
            throw new IllegalArgumentException("Input String can't be null");
        }

        str = str.toLowerCase();

        vowels = str.chars().filter(ch -> (ch == 'a' || ch == 'e' ||
            ch == 'i' || ch == 'o' || ch == 'u')).count();

        consonants = str.chars().filter(ch -> (ch != 'a' && ch != 'e' &&
                ch != 'i' && ch != 'o' && ch != 'u'))
            .filter(ch -> (ch >= 'a' && ch <= 'z')).count();

    }
}
Output:
Input text: 
Java is Popular Programming Language

Java 8, functional-style solution:
Vowels: 13
Consonants: 19

Comments