计算字符串的频率

时间:2011-10-20 03:07:14

标签: java arrays string return

我基本上想要搜索字符串的频率。例如,如果我传入单词“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。我不知道我做错了什么。

编辑:你们都是对的!多么浪费一个问题:(。

8 个答案:

答案 0 :(得分:6)

使用equals代替==

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

更好的解决方案

使用地图以频率映射单词Map<String,Integer>

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)

答案 1 :(得分:2)

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

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

答案 2 :(得分:1)

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;
}

答案 3 :(得分:0)

字符串应该与String.equals进行比较,而不是==,它会检查它们是否是相同的对象,而不是它们是否具有相同的内容

答案 4 :(得分:0)

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

答案 5 :(得分:0)

为了让您的代码能够按照其他答案进行操作,请使用.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)

答案 6 :(得分:0)

您可以使用Map将单词作为键的关键和频率作为值。然后在循环内部,尝试使用try-catch块将+ 1添加到当前单词(tryblock)的关键字,如果单词没有找到&#34; fdist.get(w)&#34;将给出nullpointerexception,然后简单地将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);
    }
}

答案 7 :(得分:0)

确定文件中单词的频率 这是java

中hashmap的基本代码
$a = @"
{
  "`$schema": "http://json-schema.org/draft-04/schema#",
  "description": "",
  "type": "object",
  "properties": {
    "definition": {
      "type": "object",
      "properties": {
        "s": {
          "type": "string",
          "minLength": 1
        }
      },
      "required": [
        "s"
      ]
    },
    "calcDefFilters": {
      "type": "object",
      "properties": {
        "l": {
          "type": "array",
          "uniqueItems": true,
          "minItems": 1,
          "items": {
            "required": [],
            "properties": {
              "m": {
                "type": "object",
                "properties": {
                  "field": {
                    "type": "object",
                    "properties": {
                      "s": {
                        "type": "string",
                        "minLength": 1
                      }
                    },
                    "required": [
                      "s"
                    ]
                  },
                  "value": {
                    "type": "object",
                    "properties": {
                      "s": {
                        "type": "string",
                        "minLength": 1
                      }
                    },
                    "required": [
                      "s"
                    ]
                  },
                  "operator": {
                    "type": "object",
                    "properties": {
                      "s": {
                        "type": "string",
                        "minLength": 1
                      }
                    },
                    "required": [
                      "s"
                    ]
                  }
                },
                "required": [
                  "field",
                  "value",
                  "operator"
                ]
              }
            }
          }
        }
      },
      "required": [
        "l"
      ]
    },
    "calculationId": {
      "type": "object",
      "properties": {
        "s": {
          "type": "string",
          "minLength": 1
        }
      },
      "required": [
        "s"
      ]
    },
    "calcDefParameters": {
      "type": "object",
      "properties": {
        "l": {
          "type": "array",
          "uniqueItems": true,
          "minItems": 1,
          "items": {
            "required": [],
            "properties": {
              "m": {
                "type": "object",
                "properties": {
                  "name": {
                    "type": "object",
                    "properties": {
                      "s": {
                        "type": "string",
                        "minLength": 1
                      }
                    },
                    "required": [
                      "s"
                    ]
                  },
                  "value": {
                    "type": "object",
                    "properties": {
                      "n": {
                        "type": "string",
                        "minLength": 1
                      }
                    },
                    "required": [
                      "n"
                    ]
                  },
                  "type": {
                    "type": "object",
                    "properties": {
                      "s": {
                        "type": "string",
                        "minLength": 1
                      }
                    },
                    "required": [
                      "s"
                    ]
                  }
                },
                "required": [
                  "name",
                  "value",
                  "type"
                ]
              }
            }
          }
        }
      },
      "required": [
        "l"
      ]
    },
    "runId": {
      "type": "object",
      "properties": {
        "s": {
          "type": "string",
          "minLength": 1
        }
      },
      "required": [
        "s"
      ]
    },
    "type": {
      "type": "object",
      "properties": {
        "s": {
          "type": "string",
          "minLength": 1
        }
      },
      "required": [
        "s"
      ]
    },
    "externalId": {
      "type": "object",
      "properties": {
        "s": {
          "type": "string",
          "minLength": 1
        }
      },
      "required": [
        "s"
      ]
    },
    "dealVersionId": {
      "type": "object",
      "properties": {
        "s": {
          "type": "string",
          "minLength": 1
        }
      },
      "required": [
        "s"
      ]
    }
  },
  "required": [
    "definition",
    "calcDefFilters",
    "calculationId",
    "calcDefParameters",
    "runId",
    "type",
    "externalId",
    "dealVersionId"
  ]
}
"@

$b = $a | ConvertFrom-Json 
$properties = $b.properties.Psobject.Members | where {$_.MemberType -eq 'NoteProperty'} | % {$_.Name}