简体   繁体   English

如何使用Java搜索用户确定的单词并计算文本文件中的出现次数?

[英]How would I search for a user determined word and count the occurrences in a text file using java?

我已经到了可以读取文件并在文件中输出实际文本的地步,但是我不太确定如何继续搜索特定单词并显示单词数。

There are many ways. 有很多方法。 If you're reading the file line-by-line, you can using the method indexOf on the String class to search each line for the text. 如果要逐行读取文件,则可以使用String类上的indexOf方法在每一行中搜索文本。 You'd need to call it repeatedly to move through the line looking for additional occurrences. 您需要反复调用它以遍历该行以查找其他事件。

See documentation on indexOf at: 请参阅indexOf上的文档,位于:

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(java.lang.String,%20int) http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(java.lang.String,%20int)

As I understand your question, if you are reading the text line after line you can use recursivity to count how many occurences of the word appear in the same line: 据我了解您的问题,如果您正在逐行阅读文本行,则可以使用递归计算该行中出现该单词的次数:

The following method counts how many times a word appears in the same line 以下方法计算单词在同一行中出现的次数

private static int numberOfLineOccurences;

public static int countNumberOfTimesInALine(String line, String word) {
    if (line.indexOf(word) == -1) {
        return numberOfLineOccurences;
    } else {
        numberOfLineOccurences++;
        if (line.indexOf(word) + word.length() >  line.length() -1     ) {
            return numberOfLineOccurences;
        }
        return countNumberOfTimesInALine(
                line.substring(line.indexOf(word) + word.length()), word );
    }

}

In order to keep track of the first occurence of my word in my file along with the number of occurence I created a WordInfo class like this: 为了跟踪单词在文件中的首次出现以及出现的次数,我创建了一个WordInfo类,如下所示:

class WordInfo {

    private int firstOccurenceLineNumber;
    private int firstOccurenceColumnNumber;
    private String word;
    private int numberOfOccurences;

    public String getWord() {
        return word;
    }

    public int getNumberOfOccurences() {
        return numberOfOccurences;
    }

    public WordInfo(String word) {
        this.word = word;
    }

    public void upOccurrence() {
        numberOfOccurences++;
    }

   public void upOccurrence(int numberOfTimes) {
        numberOfOccurences+= numberOfTimes;
    }

   public int getFirstOccurenceLineNumber() {
    return firstOccurenceLineNumber;
    }

   public void setFirstOccurenceLineNumber(int firstOccurenceLineNumber) {
       this.firstOccurenceLineNumber = firstOccurenceLineNumber;
    }

   public int getFirstOccurenceColumnNumber() {
       return firstOccurenceColumnNumber;
    }

    public void setFirstOccurenceColumnNumber(int     firstOccurenceColumnNumber) {
        this.firstOccurenceColumnNumber = firstOccurenceColumnNumber;
    }
}

Now I can create my searchWord method. 现在,我可以创建我的searchWord方法。 I give him the word to look for, the fileName and a WordInfo object to fill as input parameters 我给他寻找的单词,fileName和一个WordInfo对象作为输入参数填充

public static boolean searchWord(String word, String filePath, WordInfo wInfo) throws IOException {

    boolean result = false;
    boolean firstOccurenceFound = false;
    int lineNumber = 0;

    BufferedReader reader = new BufferedReader(new FileReader(new File(filePath)));
    String line = null;

    while ( (line = reader.readLine()) != null) {

        lineNumber++;

        numberOfLineOccurences= 0;

        if (line.indexOf(word) != -1) {
            if (!result) {
                result = true;
            }
            if (!firstOccurenceFound) {
                firstOccurenceFound = true;
                wInfo.setFirstOccurenceLineNumber(lineNumber);
                wInfo.setFirstOccurenceColumnNumber(line.indexOf(word) + 1);


            }
            wInfo.upOccurrence(countNumberOfTimesInALine(line, word));
        }


    }

    reader.close();

    return result;
}

Here is an illustration and the result below 这是一个例子,下面是结果

I have the following content in a file called DemoFile.txt 我在名为DemoFile.txt的文件中具有以下内容

测试数据示例

And I test the code using the following main method (I am looking for the word concept for example): 然后,我使用以下主要方法测试代码(例如,我正在寻找单词concept ):

public static void main(String[] args) throws IOException {
    WordInfo wInfo = new WordInfo("concept");
    if ( searchWord("concept", FILE_PATH, wInfo)) {
        System.out.println("Searching for " + wInfo.getWord());
        System.out.println("First line where found : " + wInfo.getFirstOccurenceLineNumber());
        System.out.println("First column found: " + wInfo.getFirstOccurenceColumnNumber());
        System.out.println("Number of occurrences " + wInfo.getNumberOfOccurences());


    }


}

And I obtain the following results: 并且我得到以下结果:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM