>

Saturday, March 1, 2014

Write an example that counts the number of times a particular character, such as e, appears in a file. The character can be specified at the command line.



import java.io.BufferedInputStream;
import java.io.FileInputStream;

class GetOccurrences{
    public static void main(String args[])
    {
        try{
            FileInputStream fis=new FileInputStream("C:\\Users\\SAN-MCA-ME-MTECH-MS\\Documents\\SAN\\Temp.txt");
            BufferedInputStream bis=new BufferedInputStream(fis);
                   
            int i;
            int occurs=0;
           
            while((i=bis.read())!=-1)
            {
                char a=(char)i;
               
                if(a==args[0].charAt(0))
                {
                 occurs++;
                }
            }
            System.out.println("Letter e  is "+occurs+" of times repeated in the file");
        }catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
    }
}

No comments:

Post a Comment

Please post your comments. Your comments make us to write more programs for you.