计算字符串出现的频率

发布于 2024-12-11 05:03:11 字数 756 浏览 0 评论 0原文

我本质上想搜索字符串的频率。例如,如果我传入单词“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 技术交流群。

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

发布评论

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

评论(8

熟人话多 2024-12-18 05:03:11

使用 equals 而不是 ==

if (text[i].equals(search) )
   {
       counter++;
   }

更好的解决方案

使用 Map 将单词 Map 映射为频率。

String [] words = line.split(" ");

Map<String,Integer> frequency = new HashMap<String,Integer>();

for (String word:words){

    Integer f = frequency.get(word);
    //checking null
    if(f==null) f=0;
    frequency.put(word,f+1);
}

然后您可以通过以下方式查找特定单词:

frequency.get(word)

Use equals instead of ==

if (text[i].equals(search) )
   {
       counter++;
   }

better solution

Use a Map to map the words Map<String,Integer> with frequency.

String [] words = line.split(" ");

Map<String,Integer> frequency = new HashMap<String,Integer>();

for (String word:words){

    Integer f = frequency.get(word);
    //checking null
    if(f==null) f=0;
    frequency.put(word,f+1);
}

Then you can find out for a particular word with:

frequency.get(word)
摇划花蜜的午后 2024-12-18 05:03:11

使用equals()方法来比较字符串。

if(text[i].equals(search))
{
   counter++;
}

Use equals() method to compare string.

if(text[i].equals(search))
{
   counter++;
}
雪落纷纷 2024-12-18 05:03:11
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;
/* problem: You want to be looping over splitter. */
    for (int i=0; i<text.length; i++)
    {
/* problem: splitter[i].equals(search) */
        if (text[i]==search)
        {   
            counter++;
        }   
    }
    return counter;
}
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;
/* problem: You want to be looping over splitter. */
    for (int i=0; i<text.length; i++)
    {
/* problem: splitter[i].equals(search) */
        if (text[i]==search)
        {   
            counter++;
        }   
    }
    return counter;
}
心病无药医 2024-12-18 05:03:11

为了使您的代码能够遵循其他答案,请使用 .equals 而不是 ==,但您也可以使用 apache commons lang:

StringUtils.countMatches(text, search);

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:

StringUtils.countMatches(text, search);

http://commons.apache.org/lang/
http://commons.apache.org/lang/apidocs/org/apache/commons/lang3/StringUtils.html#countMatches(java.lang.CharSequence, java.lang.CharSequence)

苄①跕圉湢 2024-12-18 05:03:11

字符串应该与 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.

樱娆 2024-12-18 05:03:11

要比较两个 String,您必须使用 equals() 方法,而不是简单的 ==

To compare two String you have to use the equals() method and not a simple ==

神爱温柔 2024-12-18 05:03:11

您可以使用 Map 将单词作为键,将单词的频率作为值。然后在循环中,尝试使用try-catch块添加+1到与当前单词(tryblock)关联的键,如果没有找到该单词“fdist.get(w)”将给出nullpointerException,然后简单地放置catch 1 为值。

Map<String,Integer> fdist = new HashMap<String,Integer>();
for(String w:s.split(" ")){
    try {
        fdist.put(word, fdist.get(w)+1);
    } catch (Exception e) {
        fdist.put(word, 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.

Map<String,Integer> fdist = new HashMap<String,Integer>();
for(String w:s.split(" ")){
    try {
        fdist.put(word, fdist.get(w)+1);
    } catch (Exception e) {
        fdist.put(word, 1);
    }
}
遇见了你 2024-12-18 05:03:11

确定文件中单词的频率。
这是java中hashmap的基本代码

File f = new File(fileName);
 Scanner s = new Scanner(f);
   Map<String, Integer> counts =
  new Map<String, Integer>();
      while( s.hasNext() ){
 String word = s.next();
 if( !counts.containsKey( word ) )
 counts.put( word, 1 );
else
 counts.put( word, 
    counts.get(word) + 1 ); 
}

Determine the frequency of words in a file.
This is basic code for hashmap in java

File f = new File(fileName);
 Scanner s = new Scanner(f);
   Map<String, Integer> counts =
  new Map<String, Integer>();
      while( s.hasNext() ){
 String word = s.next();
 if( !counts.containsKey( word ) )
 counts.put( word, 1 );
else
 counts.put( word, 
    counts.get(word) + 1 ); 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文