查找字数”- 我的代码不能正常工作

Find Word Count"- My code doesn't work properly

“查找字数统计”- 说明: 给定一个输入字符串(假设它本质上是一段文本)和一个 要查找的单词,return 该单词在输入字符串中出现的次数 成立。应该与大小写无关,并在找到匹配词时删除 space、逗号、句号、引号、制表符等。

=======================

我的代码无法正常工作。

`
String input = " It can hardly be a coincidence that no language on" +
                " Earth has ever produced the expression as pretty as an airport." +
                " Airports are ugly. Some are very ugly. Some attain a degree of ugliness" +
                " that can only be the result of a special effort. This ugliness arises " +
                "because airports are full of people who are tired, cross, and have just " +
                "discovered that their luggage has landed in Murmansk (Murmansk airport " +
                "is the only known exception to this otherwise infallible rule), and architects" +
                " have on the whole tried to reflect this in their designs. They have sought" +
                " to highlight the tiredness and crossness motif with brutal shapes and nerve" +
                " jangling colors, to make effortless the business of separating the traveller" +
                " for ever from his or her luggage or loved ones, to confuse the traveller with" +
                " arrows that appear to point at the windows, distant tie racks, or the current " +
                "position of Ursa Minor in the night sky, and wherever possible to expose the " +
                "plumbing on the grounds that it is functional, and conceal the location of the" +
                "departure gates, presumably on the grounds that they are not.";
        input = input.toLowerCase();
        String whichWord = "be";
        whichWord = whichWord.toLowerCase();
        int lastIndex = 0;
        int count = 0;

        while(lastIndex != -1){

            lastIndex = input.indexOf(whichWord,lastIndex);

            if(lastIndex != -1){
                count ++;
                lastIndex += whichWord.length();
            }
        }
        System.out.println(count);
`

在您的代码中,您没有检查完整的单词。因此,它同时匹配 'be' 和 'because'。您正在检查是否有任何子字符串包含单词 'be'。您能否使用正则表达式尝试以下解决方案?它将解决您的目的:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WordCount {

    public static void main(String[] args) {
        String input = " It can hardly be a coincidence that no language on" +
                " Earth has ever produced the expression as pretty as an airport." +
                " Airports are ugly. Some are very ugly. Some attain a degree of ugliness" +
                " that can only be the result of a special effort. This ugliness arises " +
                "because airports are full of people who are tired, cross, and have just " +
                "discovered that their luggage has landed in Murmansk (Murmansk airport " +
                "is the only known exception to this otherwise infallible rule), and architects" +
                " have on the whole tried to reflect this in their designs. They have sought" +
                " to highlight the tiredness and crossness motif with brutal shapes and nerve" +
                " jangling colors, to make effortless the business of separating the traveller" +
                " for ever from his or her luggage or loved ones, to confuse the traveller with" +
                " arrows that appear to point at the windows, distant tie racks, or the current " +
                "position of Ursa Minor in the night sky, and wherever possible to expose the " +
                "plumbing on the grounds that it is functional, and conceal the location of the" +
                "departure gates, presumably on the grounds that they are not.";

        input = input.toLowerCase();
        String whichWord = "be";
        whichWord = whichWord.toLowerCase();

        int count = 0;
        String regex = "(\W|^)" + whichWord + "(\W|$)";
        Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(input);

        while(matcher.find()) {
            count++;
        }

        System.out.println(count);
    }

}