Count Number of Vowels Java Program
Counting the number of vowels in a string is another Java Program for beginners. At first this program can just read a word because of the limitation in using the next() predefined method, but if you want to read and check the entire string together with the spaces and commas entered, use the predefined method nextline() instead.
This program use the nextLine() predefined method for better demonstration. You can also change it into next() method if you want to see the difference.
Program Algorithm:
The program that is provided in this hub will prompt the user to enter a string according to his wishes and the program would count the number of vowels regardless of how many spaces entered in the string. The vowels will be check using the charAt() predefined method and will loop it until it reach the maximum number of characters that the string has. Whether the vowels are in upper or lower cases the program will still count it, leaving no vowels behind that are uncounted.
See the output of this program. Try it also on your Java IDE. Happy Programming!
Java Source Code: Count Number of Vowels in a String
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String word;
System.out.print(“Enter a String: “);
word = input.nextLine();
int count =0;
for(int i = 0; i < word.length(); i++)
{
if(word.charAt(i) == 'a' || word.charAt(i) == 'A')
{
count++;
}
else if(word.charAt(i) == 'e' || word.charAt(i) == 'E')
{
count++;
}
else if(word.charAt(i) == 'i' || word.charAt(i) == 'I')
{
count++;
}
else if(word.charAt(i) == 'o' || word.charAt(i) == 'O')
{
count++;
}
else if(word.charAt(i) == 'u' || word.charAt(i) == 'U')
{
count++;
}
}
System.out.println("Number of vowels: " + count);
}
}
Sample Output:
Is this program useful to you? Rate it below. Any question about the program, you are welcome to ask at the comment section.
Related java Tutorials
5 Important Tips to Learn Java Programming and Other Programming Languages
Basic Knowledge Required in Programming
How to Program in Java Using Netbeans: Complete Simple Easy Steps
Java Simple Codes for Beginners
Java Tutorial for Beginners: A Beginners Guide on Learning Java Programming
Java Tutorial Examples
Java Online Traning
JAVA PROGRAMMING CBTs Video Training Tutorials 9+ Hours
Current Bid: $9.95
Java 1.1 Certification Training Guide by Cary Jardin (1998, Hardcover)
Current Bid: $3.99
Java Video
3.2″ Touch Screen 32G Quad Band Java Mobile Cell Phone Bluetooth FM, Pic/Video
Current Bid: $49.99
Motorola V620 Unlocked Bluetooth Video Camera JAVA Bluetooth Cell Phone Mint
Current Bid: $24.99
Java Book
Java Fern Narrow Great Beginner Plant easy to care for!!!
Current Bid: $2.39
Other Java Source Codes
Java Program: How to Use If Statement in Java Programming
Java Program: Using Multi If and Else Statement in Java Programming
Java Program: How to Use If-Else Statement in Java Programming
Java Program: How to Use Switch Statement in Java
Java Program: How to Parse a String into Integer in Java Programming
Java Program: Count the Number of String Characters in Java
Java Program: Reverse String in Java Using For Loop
Java Program: Palindrome Test Java Source Code
Java Program: How to Sort Numbers in an Array in a Descending Order
Java Program: How to Sort Numbers in Ascending Order
Sample Java Program for String Tokenizer
Sample Program for JOptionpane and Java Source Code for Basic Calculator
What is a Java Scanner Class and How to Use it?
What is MDAS? Sample Java Source Code for MDAS
What is a Static Method in Java?
Casting in Java
BufferedReader in Java
Fibonacci Java Program: Complete Program for Fibonacci Series
Class in Java: Learn More about Java Classes with Sample Java Codes
Java Source code: How to Add numbers inside an Array Using Recursion