计算字符串出现的频率
我本质上想搜索字符串的频率。例如,如果我传入单词“I”,则该单词在以下句子中的频率:“I去了海滩,I看到了三个人”应该是 2。我构建了这样的方法,其中我获取一个文本(任意长度),用空格将其分割成一个数组,然后循环遍历该数组,搜索每个索引是否与该单词匹配。然后,我增加频率计数器并将数字作为字符串返回。这是方法:
private int freq() {
String text = "I went to the beach and I saw three people";
String search = "I";
String[] splitter = text.split("\\s+");
int counter = 0;
for (int i=0; i<splitter.length; i++)
{
if (splitter[i]==search)
{
counter++;
}
else
{
}
}
return counter;
}
}
这是方法之外的:
String final = Integer.toString(freq());
System.out.println(final);
但是当我运行这个时,我不断得到 0 作为结果。我不知道我做错了什么。
编辑:你们都是对的!真是浪费一个问题:(。
I essentially want to search the frequency of a string. For example, if I pass in the word "I", then the frequency of the word in the following sentence: "I went to the beach and I saw three people" should be 2. I've constructed such method in which I take a text (of any length), split it into an array by the white space, and loop through the array, searching if each index matches the word. Then, I increment the frequency counter and return the number as a string. Here's the method:
private int freq() {
String text = "I went to the beach and I saw three people";
String search = "I";
String[] splitter = text.split("\\s+");
int counter = 0;
for (int i=0; i<splitter.length; i++)
{
if (splitter[i]==search)
{
counter++;
}
else
{
}
}
return counter;
}
}
This is outside the method:
String final = Integer.toString(freq());
System.out.println(final);
But as I run this, I keep getting 0 as the result. I don't know what I'm doing wrong.
EDIT: You're all correct! What a waste of a question :(.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
使用
equals
而不是==
更好的解决方案
使用 Map 将单词
Map
映射为频率。然后您可以通过以下方式查找特定单词:
Use
equals
instead of==
better solution
Use a Map to map the words
Map<String,Integer>
with frequency.Then you can find out for a particular word with:
使用
equals()
方法来比较字符串。Use
equals()
method to compare string.为了使您的代码能够遵循其他答案,请使用 .equals 而不是 ==,但您也可以使用 apache commons lang:
http://commons.apache.org/lang/
http://commons.apache.org/lang/apidocs/org/apache/commons/lang3/StringUtils.html#countMatches(java.lang.CharSequence, java.lang.CharSequence)
For you code to work follow the other answers, use a .equals instead of ==, but you could also use apache commons lang:
http://commons.apache.org/lang/
http://commons.apache.org/lang/apidocs/org/apache/commons/lang3/StringUtils.html#countMatches(java.lang.CharSequence, java.lang.CharSequence)
字符串应该与 String.equals 进行比较,而不是 == ,后者检查它们是否是相同的对象,而不是它们是否具有相同的内容。
Strings should be compared with String.equals, not ==, which checks to see if they're the same object, not if they haber the same contents.
要比较两个
String
,您必须使用equals()
方法,而不是简单的==
To compare two
String
you have to use theequals()
method and not a simple==
您可以使用 Map 将单词作为键,将单词的频率作为值。然后在循环中,尝试使用try-catch块添加+1到与当前单词(tryblock)关联的键,如果没有找到该单词“fdist.get(w)”将给出nullpointerException,然后简单地放置catch 1 为值。
You can use Map to put word as key and frequency of the word as value. Then inside the loop, try using try-catch block to add+1 to key associate with the current word (tryblock), if the word doesn't found "fdist.get(w)" will give nullpointerexception, then catch with simply put 1 for the value.
确定文件中单词的频率。
这是java中hashmap的基本代码
Determine the frequency of words in a file.
This is basic code for hashmap in java