Java Program to find the most repeated word in a text file

In this program, we need to find the most repeated word present in given text file. This can be done by opening a file in read mode using file pointer. Read the file line by line. Split a line at a time and store in an array. Iterate through the array and find the frequency of each word and compare the frequency with maxcount. If frequency is greater than maxcount then store the frequency in maxcount and corresponding word that in variable word. The content of data.txt file used in the program is shown below.

data.txt

A computer program is a collection of instructions that performs specific task when executed by a computer.

Computer requires programs to function.

Computer program is usually written by a computer programmer in programming language.

A collection of computer programs, libraries, and related data are referred to as software.

Computer programs may be categorized along functional lines, such as application software and system software.

Algorithm

  • STEP 1: START
  • STEP 2: DEFINE String line, word = “”
  • STEP 3: SET count =0, maxCount =0
  • STEP 4: DEFINE ArrayList<String> words
  • STEP 5: USE File Reader to open file in read mode.
  • STEP 6: READ line from file
  • STEP 7: By looping, CONVERT each line into lower case.
  • STEP 8: REMOVE the punctuation marks.
  • STEP 9: SPLIT the lines and STORE in array string[].
  • STEP 10: ADD all words generated in previous step into words.
  • STEP 11: SET i=0.REPEAT STEP 12 to STEP 17 UNTIL i<words.size()
  • STEP 12: SET count =1
  • STEP 13: SET j=i+1.REPEAT STEP 14 to 15 STEP UNTIL j<words.size()
  • STEP 14: IF(words.get(i).equals(words.get(j))) then count = count+1.
  • STEP 15: j = j+1
  • STEP 16: IF count>maxCount
    then
    maxCount = count
    word =words.get(i)
  • STEP 17: i=i+1
  • STEP 18: PRINT word
  • STEP 19: END

Program:

  1. import java.io.BufferedReader;    
  2. import java.io.FileReader;    
  3. import java.util.ArrayList;    
  4.      
  5. public class MostRepeatedWord {    
  6.         
  7.     public static void main(String[] args) throws Exception {    
  8.         String line, word = “”;    
  9.         int count = 0, maxCount = 0;    
  10.         ArrayList<String> words = new ArrayList<String>();    
  11.             
  12.         //Opens file in read mode    
  13.         FileReader file = new FileReader(“data.txt “);    
  14.         BufferedReader br = new BufferedReader(file);    
  15.             
  16.         //Reads each line    
  17.         while((line = br.readLine()) != null) {    
  18.             String string[] = line.toLowerCase().split(“([,.\\s]+) “);    
  19.             //Adding all words generated in previous step into words    
  20.             for(String s : string){    
  21.                 words.add(s);    
  22.             }    
  23.         }    
  24.             
  25.         //Determine the most repeated word in a file    
  26.         for(int i = 0; i < words.size(); i++){    
  27.             count = 1;    
  28.             //Count each word in the file and store it in variable count    
  29.             for(int j = i+1; j < words.size(); j++){    
  30.                 if(words.get(i).equals(words.get(j))){    
  31.                     count++;    
  32.                 }     
  33.             }    
  34.             //If maxCount is less than count then store value of count in maxCount     
  35.             //and corresponding word to variable word    
  36.             if(count > maxCount){    
  37.                 maxCount = count;    
  38.                 word = words.get(i);    
  39.             }    
  40.         }    
  41.             
  42.         System.out.println(“Most repeated word: ” + word);    
  43.         br.close();    
  44.     }    
  45. }    

Output:

Most repeated word: computer

Leave a Comment