
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Program to Count the Number of Vowels and Consonants in a Sentence
In this article, we will understand how to count the vowels and consonants in Java. Alphabets that include 'a' 'e' 'i' 'o' 'u' are called Vowels and all other alphabets are called Consonants.
Below is a demonstration of the same −
Input
Suppose our input is −
Hello, my name is Charlie
Output
The desired output would be −
The number of vowels in the statement is: 8 The number of vowels in the Consonants is: 12
Algorithm
Step1- Start Step 2- Declare two integers: vowels_count, consonants_count and a string my_str Step 3- Prompt the user to enter a string value/ define the string Step 4- Read the values Step 5- Run a for-loop, check each letter whether it is a consonant or an vowel. Increment the respective integer. Store the value. Step 6- Display the result Step 7- Stop
Example 1
Here, the input is being entered by the user based on a prompt. You can try this example live in ourcoding ground tool .
import java.util.Scanner; public class VowelAndConsonents { public static void main(String[] args) { int vowels_count, consonants_count; String my_str; vowels_count = 0; consonants_count = 0; Scanner scanner = new Scanner(System.in); System.out.println("A scanner object has been defined "); System.out.print("Enter a statement: "); my_str = scanner.nextLine(); my_str = my_str.toLowerCase(); for (int i = 0; i < my_str.length(); ++i) { char ch = my_str.charAt(i); if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { ++vowels_count; } else if ((ch >= 'a' && ch <= 'z')) { ++consonants_count; } } System.out.println("The number of vowels in the statement is: " + vowels_count); System.out.println("The number of vowels in the Consonants is: " + consonants_count); } }
Output
A scanner object has been defined Enter a statement: Hello, my name is Charlie The number of vowels in the statement is: 8 The number of vowels in the Consonants is: 12
Example 2
Here, the integer has been previously defined, and its value is accessed and displayed on the console.
public class VowelAndConsonents { public static void main(String[] args) { int vowels_count, consonants_count; vowels_count = 0; consonants_count = 0; String my_str = "Hello, my name is Charie"; System.out.println("The statement is defined as : " +my_str ); my_str = my_str.toLowerCase(); for (int i = 0; i < my_str.length(); ++i) { char ch = my_str.charAt(i); if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { ++vowels_count; } else if ((ch >= 'a' && ch <= 'z')) { ++consonants_count; } } System.out.println("The number of vowels in the statement is: " + vowels_count); System.out.println("The number of vowels in the Consonants is: " + consonants_count); } }
Output
The statement is defined as : Hello, my name is Charie The number of vowels in the statement is: 8 The number of vowels in the Consonants is: 11
- Related Articles
- Swift Program to Count the Number of Vowels and Consonants in a Sentence
- Kotlin Program to Count the Number of Vowels and Consonants in a Sentence
- Haskell Program to Count the Number of Vowels and Consonants in a Sentence
- How to Count the Number of Vowels and Consonants in a Sentence in Golang?
- Java program to count the number of consonants in a given sentence
- Java program to count the number of vowels in a given sentence
- C# Program to count number of Vowels and Consonants in a string
- How to count number of vowels and consonants in a string in C Language?
- C Program to count vowels, digits, spaces, consonants using the string concepts
- C++ Program to Find the Number of Vowels, Consonants, Digits and White Spaces in a String
- Java Program to count vowels in a string
- Java Program to count all vowels in a string
- Frequency of vowels and consonants in JavaScript
- Python program to count the number of vowels using set in a given string
- Python program to count the number of vowels using sets in a given string

Advertisements