Java program that count the total number of words,characters,lines in a given file - Code

Latest

programs

Monday, October 10, 2011

Java program that count the total number of words,characters,lines in a given file


//program that count the total number of words,characters,lines in a given file
import javax.swing.*;
import java.io.*;
import java.util.*;
public class count
 {
    public static void main(String args[])
     {
       try
       {
    String s=JOptionPane.showInputDialog("Enter the file name : ");
FileInputStream f=new FileInputStream(s);
DataInputStream d=new DataInputStream(f); 
String data;
StringTokenizer st;
int words=0,chars=0,i=0;
while((data=d.readLine())!=null)
  {
i++;
st=new StringTokenizer(data);
words+=st.countTokens();
chars+=data.length();
  }
System.out.println("total words \n" +words);
System.out.println("total chras \n" +chars);
System.out.println("total lines \n"  +i );
f.close();
      }
         catch(Exception e)
            {
        System.out.println("err"+e);
        }
   }
}

No comments:

Post a Comment