Search Here

Java program for counting number of vowels in a text file


/* Count number of vowels in a text file */

import java.io.*;
class Main
{
 public static void main(String args[])
 {
  BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  FileInputStream fstream;
  DataInputStream in;
  try
  {
   System.out.print("Enter file name: ");
   String fname=br.readLine();
   
   fstream=new FileInputStream(fname);
   in=new DataInputStream(fstream);
   
   int vCount=0;
   while(in.available()!=0)
   {
    String st=in.readLine();
    String stLower=st.toLowerCase();
    
    for(int i=0;i<stLower.length();i++)
    {
     char ch=stLower.charAt(i);
     if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
      vCount++;
    }
   }
   System.out.println("No. of vowels in "+fname+": "+vCount);
   in.close(); 
  }
  catch(Exception e)
  {
   System.err.println(e);
  }
 }
}

JAVA program to count the number of vowels in a text file
Output

No comments:

Post a Comment