---
title: "Find the Frequency of a sentence"  
description: "In this blog I am trying to make a small demo which is count the frequency of a sentence accepted by user using java.  import java.nio.CharBuffer;  im"  
author: "Vijay Shukla"  
published: 2013-10-04  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/601/find-the-frequency-of-a-sentence  
category: "java"  
tags: ["java"]  
reading_time: 2 minutes  

---

# Find the Frequency of a sentence

In this [blog](https://www.mindstick.com/articles/12705/myths-and-misconception-about-blog) I [am trying](https://answers.mindstick.com/qa/36834/which-two-programming-languages-should-i-master-in-if-i-am-trying-to-get-into-google-or-facebook) to make a small [demo](https://www.mindstick.com/forum/157370/how-to-swap-neighbor-characters-in-string-e-g-string-demo-would-be-edom) which is [count](https://www.mindstick.com/forum/157774/selecting-count-with-distinct) the frequency of a sentence [accepted](https://yourviews.mindstick.com/view/80674/whatsapp-accepted-as-most-popular-app-worldwide) by [user](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) using java.

\

```
import java.nio.CharBuffer;import java.nio.MappedByteBuffer;import java.nio.channels.FileChannel;import java.nio.charset.Charset;import java.nio.charset.CharsetDecoder;import java.util.Map;import java.util.TreeMap;import java.util.regex.Matcher;import java.util.regex.Pattern;import java.util.LinkedHashMap;import java.util.Scanner;
public class WordFreq{                public static void main(String args[]) throws Exception                {                                Scanner in = new Scanner(System.in);                                System.out.print("Enter String : ");                        String text=in.nextLine();                text = text.replaceAll("( )+", " ");                                wordFrequencyCount(text);
                }                public static void wordFrequencyCount(String s)
{    int count=0;    String sentance[]=sortWords(s);    System.out.println("Word\t\tfrequency");    for(int i=0;i<sentance.length;)    {        count=1;        for(int index=i+1;index<sentance.length;index++)        {            if(sentance[i].compareTo(sentance[index])==0)            {
                count++;            }            else            {                break;            }        }
          System.out.println(""+sentance[i] +"\t\t"+count);
        i+=count;
    }
}public static String[] sortWords(String s1){
    String s[]=s1.split(" ");    for( int i=0;i<s.length-1;i++)        {            for( int j=0;j<s.length-i-1;j++)            {                if(s[j].compareTo(s[j+1])>0)                {                    String g=s[j+1];                    s[j+1]=s[j];                    s[j]=g;
                }            }         }    return s;
}
}
```

\

##### Output: -

![Find the Frequency of a sentence](https://www.mindstick.com/blogs/af36e4e7-9003-4ce1-83f2-0de4bfdae0e5/images/96033bad-b1fe-4db1-993e-ad528ad8909d.png)

---

Original Source: https://www.mindstick.com/blog/601/find-the-frequency-of-a-sentence

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
