Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java String-examples Java Count Vowels

Related Tutorials


 
 

Share on Google+Share on Google+

Java Count Vowels

Advertisement
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 java.io.*; 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.io.*;
import java.util.*;

public class CountVowels{

public static void main(String args[])throws IOException{
  BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
  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') {
  count++;
  }
  }
  System.out.println("There are" " " + count + " " "vowels");
}

Output of the program:

C:\unique>javac CountVowels.java

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

Download this example.

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 18, 2007

Related Tutorials

Discuss: Java Count Vowels   View All Comments

Post your Comment


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

GOOD WAY OF LERNING
John
March 28, 2011
Computer Java Programming

Good way of learning
nick
March 8, 2012
fi

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??
Scott
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.
Rohit
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
jamie
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
Hemalatha
November 26, 2013
Compare List and String

Can we retrieve the vowels using List?
DMCA.com