Ram Ki
Ram Ki

Reputation: 282

How to find duplicate words without counting the original word from a file in java?

 import java.io.BufferedReader;   
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FilenameFilter;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 import org.apache.commons.io.FileUtils;
 public class sample{
    public static void main(String[] args) throws FileNotFoundException, IOException {

    FilenameFilter filter = new FilenameFilter() {
        public boolean accept(File dir, String name) {
            return name.endsWith(".txt");
        }
    };

    File folder = new File("E:\\testfolder\\");
    File[] list Of Files = folder.listFiles(filter);

    for (int i = 0; i < list Of Files.length; i++) {
        File file1 = list Of Files[i];
        try {
            String content = FileUtils.readFileToString(file1);

        } catch (IOException e) {

            e.printStackTrace();
        }

        BufferedReader ins = null;
        try {
            ins = new BufferedReader (
                    new InputStreamReader(
                        new FileInputStream(file 1)));
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        }

        String message = org.apache.commons.io.IOUtils.toString(ins);
        String[] string array = message.split(" "); 
        List<String> list = new ArrayList<String>(Arrays.asList(string array)); 
    //  System.out.println("Repeated words found in the file"); 
        Set<String> unique Set = new HashSet<String>(list); 
        for (String temp : unique Set) { 
                if ( (Collections.frequency(list, temp)) >= 2 ){
        System.out.println(temp+"="+Collections.frequency(list, temp));
        //  System.out.println(temp);
         int occurrences = Collections.frequency(list, 2);

        } 
        }
        }}}

So far I tried and I could not get my output Here is my logic.

These are the String or words from a file. ram ram ram sam sam sam man man

In this example ram sam man these are original words. Remaining ram, ram,sam, sam,man are duplicate words aka repeated words. So Total count of words is 8 Total count of duplicate words is 5 Total no of remaining words is 3.

But I am getting my output as No of repeated words=3(ram=3,sam=3,man=2)

Here is my code above I am a beginner to java. Any suggestions are welcomed.:)

Upvotes: 2

Views: 149

Answers (1)

user5933314
user5933314

Reputation:

Collections.frequency(list, temp) - 1 

instead of

Collections.frequency(list, temp)

Study about collections.frequency in internet you can get the answer

Upvotes: 1

Related Questions