Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- class Counter
- {
- public static void main(String[] args) throws NullPointerException
- {
- System.out.println("Enter the number of words to find its frequency:");
- Scanner scan = new Scanner(System.in);
- int k=scan.nextInt();//Number of top words to be found is stored here
- Scanner scan1 = new Scanner(System.in);
- System.out.println("Enter the file path to analyse the words:");
- String path=scan1.nextLine();// Directory path will be stored here
- File folder = new File(path);
- File[] listOfFiles = folder.listFiles();//To get the list of file-names present in the path
- BufferedReader br = null;
- String words[]=null;
- String line="";
- String files;
- String word;
- int count=0;
- // List<String> list = new ArrayList<String>();
- for(File f:listOfFiles) {
- if (f.isFile()) {
- files = f.getName();
- if (files.endsWith(".txt") || files.endsWith(".TXT")) {
- try{
- br=new BufferedReader(new FileReader(f));
- try{
- while((line=br.readLine())!=null){
- line = line.toLowerCase(); // convert to lower case
- words = line.split("\\s+");
- }
- java.util.Arrays.sort(words);
- for(int i=0;i<words.length;i++) {
- word = words[i];
- for(int j=0;j<words.length;j++) {
- if(words[j+1].equals(word)){
- count++;
- }
- }
- }
- System.out.println(count);
- }
- catch(IOException e){
- System.out.println("I am sorry:"+e);
- }
- }
- catch(FileNotFoundException e){
- System.out.println("Here i have got"+e.getMessage());
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement