Write a java program to find the highest repeating word from a text file

We have to write a java program to find the highest repeating word from a text file. This is also known as Word Count Problem. 

Write an algorithm to find the highest repeating word from a text file:

  1. First, we will open the desired file. For that, we will use FileReader class. Create an object of FileReader class and pass the file name as an argument.
  2. Secondly, create an object of BufferedReader class and pass the FileReader object as an argument.
  3. Now we will declare an array list ( of string type ) that will hold the words present in the file.
  4. Now, we will iterate through a loop to find the words. For this, create a while loop that will extract each line from the file using the readLine() method. We will then create a string array that extracts the words from each line by splitting the line by space. Now that we have extracted all the words from the file, we will simply add these words to our array list.
  5. Initialize a max variable by 0.
  6. Next, create a for loop which runs from i=0 to i=words.length()-1. Initialize a count variable by 1. This count variable holds the occurrence of the word i. Create a nested for loop which runs from j=i+1 to j=words.length()-1. If the word i is equals to the word j, increment the count variable by 1. Close the inner for loop.
  7. Now, we will find the highest occurring word. For this, if the count of the word obtained by the inner for loop is found to be greater than the max variable, assign the value of count to the max variable. Thus, the max variable will hold the maximum occurring word till the current iteration. Also, save the maximum occurring word in a variable named maxword. Close the outer for loop.
  8. After the outer for loop has finished executing, the maxword variable holds the maximum occurring word in the text file and the max variable holds the occurrence of that word. Now, simply print them.

Write a program to find the highest repeating word from a text file:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class WordCount 
{
    public static void main(String[] args) throws FileNotFoundException, IOException 
    {
        FileReader f=new FileReader("TextFile");
        BufferedReader b=new BufferedReader(f);
        ArrayList <String> words=new ArrayList<String>();
        String line, maxword="";
        while((line=b.readLine())!=null)
        {
            String s[]=line.split(" ");
            for(String t:s) words.add(t);   
        }
        int max=0;
        for(int i=0;i<words.size();i++)
        {
            int count=1;
            for(int j=i+1;j<words.size();j++)
            {
                if(words.get(i).equals(words.get(j))) count++;
            }
            if(count>max)
            {
                max=count;
                maxword=words.get(i);
            }
        }
        System.out.println("The highest occuring word is: "+maxword+". It occured "+max+" times.");
    }
}

The output of the program to find the highest occurring word from a text file: