在文本文件中查找重复次数最多的单词的Java程序

1 说明

在此程序中,我们需要找到给定文本文件中出现次数最多的单词。这可以通过使用文件指针以读取模式打开文件来完成。逐行读取文件。一次分割一行并存储在数组中。遍历数组,找到每个单词的频率,然后将频率与maxcount比较。如果频率大于maxcount,则将频率存储在maxcount中,并将相应的字存储在变量字中。该程序中使用的data.txt文件的内容如下所示。

data.txt内容如下:

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.

2 算法思路

  • 步骤1:开始
  • 步骤2: 定义String line, word = ""
  • 步骤3: 设置count =0, maxCount =0
  • 步骤4:定义ArrayList<String> words
  • 步骤5:使用File Reader以读取模式打开文件。
  • 步骤6:从文件读取行
  • 步骤7:通过循环,将每行转换为小写形式。
  • 步骤8:删除标点符号。
  • 第9步:分割行并将其存储在数组string []中。
  • 步骤10:将上一步中生成的所有单词添加到单词中。
  • 步骤11:设置i = 0。直到i <words.size()重复步骤12至步骤17
  • 步骤12: SET计数= 1
  • 步骤13:设置j = i + 1。重复步骤14至15直到j <words.size()
  • 步骤14: IF(words.get(i).equals(words.get(j)))然后count = count + 1。
  • 步骤15: j = j + 1
  • 步骤16:如果count> maxCount,

    maxCount = count
    word = words.get(i)
  • 步骤17: i = i + 1
  • 步骤18:打印word
  • 步骤19:结束

3 程序实现

/**
 * 一点教程网: http://www.yiidian.com
 */
import java.io.BufferedReader;    
import java.io.FileReader;    
import java.util.ArrayList;    
     
public class MostRepeatedWord {    
        
    public static void main(String[] args) throws Exception {    
        String line, word = "";    
        int count = 0, maxCount = 0;    
        ArrayList<String> words = new ArrayList<String>();    
            
        //Opens file in read mode    
        FileReader file = new FileReader("data.txt ");    
        BufferedReader br = new BufferedReader(file);    
            
        //Reads each line    
        while((line = br.readLine()) != null) {    
            String string[] = line.toLowerCase().split("([,.\\s]+) ");    
            //Adding all words generated in previous step into words    
            for(String s : string){    
                words.add(s);    
            }    
        }    
            
        //Determine the most repeated word in a file    
        for(int i = 0; i < words.size(); i++){    
            count = 1;    
            //Count each word in the file and store it in variable count    
            for(int j = i+1; j < words.size(); j++){    
                if(words.get(i).equals(words.get(j))){    
                    count++;    
                }     
            }    
            //If maxCount is less than count then store value of count in maxCount     
            //and corresponding word to variable word    
            if(count > maxCount){    
                maxCount = count;    
                word = words.get(i);    
            }    
        }    
            
        System.out.println("Most repeated word: " + word);    
        br.close();    
    }    
}    

以上代码输出结果为:

Most repeated word: computer

 

热门文章

优秀文章