Count Repeated Words in a Paragraph by Using Java

public class countstring {
    public static void main(String args[])
    {
        String textars="This text is to check the repeated strings and their count, text to check, text to check, text to check, text to check";
        String textars1=textars.toLowerCase(); //to count string that is in uppercase or lowercase
        String string2[]=textars1.split("[(' '),.-]"); 
        HashMap<String, Integer> uniques = new HashMap<String, Integer>();
        for (String word : string2)
            {
            // ignore words 2 or less characters long
            if (word.length() <= 2) //to eliminate to, is, it etc
                {
                 continue;
                }
        Integer existingCount = uniques.get(word);
        uniques.put(word, (existingCount == null ? 1 : (existingCount + 1)));
            }
        
        Set<Map.Entry<String, Integer>> uniqueSet = uniques.entrySet();
        boolean first = true;
        for (Map.Entry<String, Integer> entry : uniqueSet)
            {
                        
                if (entry.getValue() > 1)
                    { 
                            System.out.println(entry.getKey()+" "+entry.getValue());
                    }
            }
    
    }
}

Youtube: Video Tutorial

By Sri

Leave a Reply

Your email address will not be published. Required fields are marked *