字符串中单词的出现次数(字数统计)

发布于 2024-08-15 16:45:48 字数 647 浏览 3 评论 0原文

我坚持在字符串中写入单词出现次数。 我得到了一些提示(在任务注释中)可以使用compareToIgnoreCase。所以我尝试了这样的事情:

splitwords = StringCont.split("\\s");
for(int i=0; i<splitwords.length; i++)
{
    if(splitwords[1].compareToIgnoreCase(splitwords[i]) == 0)
        splitcount++;
}

这当然是我能做的,而且可能是坏方法。当我运行代码时,有时会出现数组异常,有时会运行。 缺少的是:遍历所有单词并检查它们并跳过已经计算过的单词。 我很乐意在这方面获得任何帮助,以便可以继续前进并了解如何编码。谢谢:)

编辑:看来我没有足够清楚地解释问题,但我得到了关于地图对象的很好的答案,它可以轻松地将我需要的东西组合在一起。我不知道地图。 所以是的,我试图找到每个给定单词在字符串中出现的次数。

tangens:它应该意味着取第一个单词(其中第一个空格) splitwords[1] 并将其与字符串 splitwords[i] 中的所有其他单词进行比较,如果它是 0(等于),则 count++。

Esko:确实有像句子中那样的空格。但我仍然得到这个例外。 我不知道为什么要通过。

Im stuck on writing Word occurrence in a string.
I got some tip(in task notes) to use is compareToIgnoreCase. so I tried something like this:

splitwords = StringCont.split("\\s");
for(int i=0; i<splitwords.length; i++)
{
    if(splitwords[1].compareToIgnoreCase(splitwords[i]) == 0)
        splitcount++;
}

It is of course just what I can do and probably bad way. When I run the code, I get sometimes out of array exeption and sometimes it runs.
What is missing is: go through all words and check them and skip the words which were already counted.
I will be happy to get any help with this so can move along and understand how it can be coded. Thank you :)

Edit: It seems I did not explain the problem enough clearly, but I get nice answer about the map object which easily put together what I needed. I did not know about map.
So yea, I was trying to find the number of times every given word is found in the string.

tangens: it should mean-take the first word(where first whitespace is) splitwords[1] and compare it to all other words in string splitwords[i] and if it is 0(equals), then count++.

Esko: there indeed are white spaces like in sentence. But I still got this exeption.
I dont know why thru.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

时光暖心i 2024-08-22 16:45:48

将单词及其计数存储在 Map

String[] words = string.toLowerCase().split("\\s+");
Map<String, Integer> wordCounts = new HashMap<String, Integer>();

for (String word : words) {
    Integer count = wordCounts.get(word);
    if (count == null) {
        count = 0;
    }
    wordCounts.put(word, count + 1);
}

请注意,我在 split() 之前调用了 toLowerCase() ,因为您似乎希望不区分大小写。

Store the words and their counts in a Map.

String[] words = string.toLowerCase().split("\\s+");
Map<String, Integer> wordCounts = new HashMap<String, Integer>();

for (String word : words) {
    Integer count = wordCounts.get(word);
    if (count == null) {
        count = 0;
    }
    wordCounts.put(word, count + 1);
}

Note that I called toLowerCase() before split() as you seem want to have case insensitivity.

遇到 2024-08-22 16:45:48

您是在寻找句子中某个单词的出现次数,还是句子中所有单词的累计字数?根据您的示例代码,我希望是前者,但您的解释让我想到后者。

无论如何,有一点很明显:如果输入字符串中没有任何空格,String#split() 返回一个仅包含一个元素的数组 - 原始字符串本身。由于数组索引从零开始,splitwords[1] 会导致 ArrayIndexOutOfBoundsException,因为唯一可用的字符串位于索引 [0] 中。

Are you looking for the occurrence of a certain word in sentence or the cumulated word count of the all the words in sentence? Based on your sample code I'd expect the former but your explanation makes me think of the latter.

In any case, something obvious: If there aren't any whitespaces in the input String, String#split() returns an array containing exactly one element - the original String itself. Since array indexes start from zero, that splitwords[1] is causing the ArrayIndexOutOfBoundsException because the only String available is in index [0].

甜宝宝 2024-08-22 16:45:48

您可以使用集合并将每个单词存储到其中,最后获取集合的大小。

当然,您会得到不同单词的数量,而不是单词的数量

You can use a Set and store each word into and at the end get the size of the set.

You will get the number of different word not the number of word of course.

囚我心虐我身 2024-08-22 16:45:48

如果你想统计单词出现的次数,你应该使用HashMap。您可以在那里为找到的每个单词存储一个计数器。

If you want to count word occurence, you should use a HashMap<String, Integer>. There you can store a counter for each word found.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文