Advertisement
Dinesh_Raja

Counter.java

Dec 23rd, 2013
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.67 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. class Counter
  4. {
  5.  public static void main(String[] args) throws NullPointerException
  6.   {
  7.   System.out.println("Enter the number of words to find its frequency:");
  8.   Scanner scan = new Scanner(System.in);
  9.   int k=scan.nextInt();//Number of top words to be found is stored here
  10.   Scanner scan1 = new Scanner(System.in);
  11.   System.out.println("Enter the file path to analyse the words:");
  12.   String path=scan1.nextLine();// Directory path will be stored here
  13.   File folder = new File(path);
  14.   File[] listOfFiles = folder.listFiles();//To get the list of file-names present in the path
  15.   BufferedReader br = null;
  16.   String words[]=null;
  17.   String line="";
  18.   String files;
  19.   String word;
  20.   int count=0;
  21.  // List<String> list = new ArrayList<String>();
  22.     for(File f:listOfFiles) {
  23.         if (f.isFile()) {
  24.         files = f.getName();
  25.                 if (files.endsWith(".txt") || files.endsWith(".TXT")) {
  26.                          try{
  27.              br=new BufferedReader(new FileReader(f));
  28.                    try{
  29.                                   while((line=br.readLine())!=null){
  30.                                         line = line.toLowerCase(); // convert to lower case
  31.                                          words = line.split("\\s+");
  32.                                                                 }
  33.                                        java.util.Arrays.sort(words);
  34.  
  35.  
  36.                                    for(int i=0;i<words.length;i++) {
  37.                                                  word = words[i];
  38.                                    for(int j=0;j<words.length;j++) {
  39.                                       if(words[j+1].equals(word)){
  40.                                                     count++;
  41.                                                   }
  42.                                            
  43.                                                           }
  44.                                               }
  45.                                       System.out.println(count);
  46.  
  47.  
  48.  
  49.                                              
  50.                                      }
  51.                                       catch(IOException e){
  52.                                            System.out.println("I am sorry:"+e);
  53.                                                            }
  54.                                                                            }
  55.                            catch(FileNotFoundException e){
  56.                              System.out.println("Here i have got"+e.getMessage());
  57.                                                           }
  58.  
  59.                                      }
  60.                        }
  61.                   }
  62.           }
  63.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement