Write a Java Program to Count Frequency or Occurrance of each Word in String

By | 17.03.2017

Java Program to Count Frequency or Occurrance of each Word in String

Write a Java Program to Count Frequency or Occurrance of each Word in String. Here’s a Simple Program To Count Frequency of words in String in Java Programming Language.

A word is a sequence of one or more non-space character i.e. any character other than ” (empty String). This should be your method signature .In order to implement this method we need to assume that two words are separated by space.

We also need to ignore leading, trailing and multiple spaces between words. One way to solve this problem is  to split String by space and then count number of parts.


1. Start

2. Accept a sentence from the user.

3. Extract a Word.

4. Count the no. of times it occurs in the sentence.

5. Print its frequency.

6. Repeat Steps 3 to 6 till all the frequencies are printed.

7. End

Following Java Program ask to the user to enter a string to find the frequency of all the words present in the string and display the frequency of all the words one by one on the screen.

Below is the source code for Java Program to Count Frequency or Occurrance of each Word in String which is successfully compiled and run on Windows System to produce desired output as shown below :


import java.util.Scanner;

public class Count_Words

    public static void main(String[] args)
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter any sentence below : ");
        String text = sc.nextLine();
        String[] keys = text.split(" ");
        String[] uniqueKeys;
        int count = 0;
        uniqueKeys = getUniqueKeys(keys);

        for(String key: uniqueKeys)
            if(null == key)
            for(String s : keys)
            System.out.println("Count of [ "+key+" ] is : "+count);

    private static String[] getUniqueKeys(String[] keys)
        String[] uniqueKeys = new String[keys.length];

        uniqueKeys[0] = keys[0];
        int uniqueKeyIndex = 1;
        boolean keyAlreadyExists = false;

        for(int i=1; i<keys.length ; i++)
            for(int j=0; j<=uniqueKeyIndex; j++)
                    keyAlreadyExists = true;

                uniqueKeys[uniqueKeyIndex] = keys[i];
            keyAlreadyExists = false;
        return uniqueKeys;


Enter any sentence below : 

This is programming website whose name is CodezClub

Count of [ This ] is : 1
Count of [ is ] is : 2
Count of [ programming ] is : 1
Count of [ website ] is : 1
Count of [ whose ] is : 1
Count of [ name ] is : 1
Count of [ CodezClub ] is : 1
4.2 10 votes
Article Rating
Category: Java Programming String Programs Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Notify of

Newest Most Voted
Inline Feedbacks
View all comments
kanu priya

Thank you so much it clearly demonstrate each and ever step.it helps me a lot

Last edited 3 years ago by kanu priya
Java programs

We can use the split() of string. This method returns an array of Strings, after splitting string based of given regex(delimiters). To fine the count of words in sentence, we will find the length of String array returned by split method. Full java program to count words in a sentence