1. Introduction
In this blog post, we are going to develop a Java program that counts the number of vowels and consonants in a given string. Vowels in the English alphabet are a, e, i, o, and u (both lowercase and uppercase), and the rest of the letters are considered consonants.
2. Program Steps
1. Import the Scanner class from the java.util package for user input.
2. Define the main class named VowelConsonantCounter.
3. Inside the main class, define the main method.
4. Inside the main method, create an object of the Scanner class to take user input.
5. Prompt the user to enter the string.
6. Initialize two variables, one for counting vowels and one for counting consonants.
7. Iterate over each character of the string.
8. Check whether each character is a vowel or a consonant and increment the respective counters.
9. Print the count of vowels and consonants in the string.
3. Code Program
import java.util.Scanner; // 1. Importing Scanner class for user input
public class VowelConsonantCounter { // 2. Defining the main class
public static void main(String[] args) { // 3. Defining the main method
Scanner input = new Scanner(System.in); // 4. Creating Scanner object to take user input
System.out.print("Enter the string: "); // 5. Prompting user to enter the string
String str = input.nextLine(); // Storing the user input in variable str
int vowelCount = 0; // 6. Initializing the vowelCount variable with 0
int consonantCount = 0; // 6. Initializing the consonantCount variable with 0
// 7. Iterating over each character of the string
for (char c : str.toCharArray()) {
if (Character.isLetter(c)) { // Check if the character is a letter
// 8. Checking whether each character is a vowel or a consonant
if ("AEIOUaeiou".indexOf(c) != -1) {
vowelCount++; // Increment the vowel counter
} else {
consonantCount++; // Increment the consonant counter
}
}
}
System.out.println("Number of vowels: " + vowelCount); // 9. Printing the count of vowels
System.out.println("Number of consonants: " + consonantCount); // 9. Printing the count of consonants
input.close(); // Closing the Scanner object to avoid memory leak
}
}
Output:
Enter the string: Programming is Fun! Number of vowels: 5 Number of consonants: 12
4. Step By Step Explanation
– Step 1: The Scanner class from java.util package is imported for user input.
– Step 2: The main class named VowelConsonantCounter is defined.
– Step 3: The main method, which is the entry point of the program, is defined inside the main class.
– Step 4: A Scanner object named "input" is created to facilitate user input.
– Step 5: The user is prompted to enter a string, and the input Programming is Fun! is stored in the variable str.
– Step 6: Two variables, vowelCount and consonantCount, are initialized to 0 to store the count of vowels and consonants respectively.
– Step 7: The program then iterates over each character of the string str.
– Step 8: For each character, it checks whether it is a vowel or a consonant by searching in the string "AEIOUaeiou", and increments the respective counters.
– Step 9: Finally, the program prints out the count of vowels 5 and consonants 12 in the given string.
– Lastly, the Scanner object is closed to prevent any potential memory leak.