Java Count Vowels

In this program you will learn how to count vowels in a String.

Java Count Vowels


In this program you will learn how to count vowels in a String. Here you will be asked to enter a String of your own choice and then you will get the number of vowels from that String.

Description of the code:

In the program code given below, you will be asked to enter the String first as can be observed from the method of*; package which is BufferedReader();. The compiler will read the String once you enter it through readLine(); method. Then we have taken a variable- count = 0. Now, we have applied a loop here which will go up to the String's length and will find the number of vowels one by one as shown in the output.

The code of the program is given below:

import java.lang.String;
import java.util.*;

public class CountVowels{

public static void main(String args[])throws IOException{
  BufferedReader bf = new BufferedReader(new InputStreamReader(;
  System.out.println("Enter the String:");
  String text = bf.readLine();
  int count = 0
  for (int i = 0; i < text.length(); i++) {
  char c = text.charAt(i);
  if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u') {
  System.out.println("There are" " " + count + " " "vowels");

Output of the program:


C:\unique>java CountVowels
Enter the String:
This is a new world
There are 5 vowels

Download this example.

Share on Google+Share on Google+

Java Count Vowels

Posted on: August 18, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Java Count Vowels   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
March 28, 2011
Computer Java Programming

March 28, 2011
Computer Java Programming

Good way of learning
March 8, 2012

This doesn't take case into concern. an uppercase vowel would not be counted
Ravi Raj
March 17, 2012
count specific number of vowel

.how can i count specific number of vowel in a string??
March 18, 2012
Upper Case Vowels

If there are any vowels in upper case this will not find them, you need to include checks for AEIOU as well as aeiou.
December 1, 2012
java count vowels

u should use hastset collection because if u r entering aaaaaaa then it will give no of vowels 7 and give vowels are aaaaaa rather than only a... so use HashSet<Character> ch = new HashSet<Character>(); and in for loop use String res = "" + text.charAt(i); for(Character c:res.toCharArray()) { if(!ch.contains(c)) { ch.add(c); System.out.print(c); } } now it will give only-a rather than aaaaaa
April 15, 2013
to clear any confusion

you should put .toLowerCase()after c = text and before .length(); i++ because if you put a capital vowel like Hi my name is Alan it will only give you 5 vowels not 6 because it wont read the A at the beginning of Alan
November 26, 2013
Compare List and String

Can we retrieve the vowels using List?