计算字符串中字符的出现次数

时间:2013-05-02 20:59:04

标签: java

我有以下程序来计算字符串中字符的出现次数。例如,给定一个字符串 - my name is stack overflow,我希望输出为

f 1 e 2 c 1 a 2 n 1 o 2 l 1 m 2 k 1 i 1 w 1 v 1 t 1 s 2 r 1 y 1

然而,似乎有些事情是错误的,我无法弄清楚是什么。另外,请注意 - 我确实看到过去发布的一些程序。 Possible duplicate。但我想帮助解决我的具体问题,而不是使用别人的解决方案。

以下是代码:

//Count the occurence of a character in a string
package strings;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CharacterOccurenceInAString {
    private static Map<Integer, Character> str = new HashMap<Integer, Character>();
    private static List<Character> charList = new ArrayList<Character>();
    private static Character value;

    public static void main(String[] args) {
        BufferedReader br = null;
        String input = "";
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Please enter a string");
            input = br.readLine();
            charOccurence(input);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    public static void charOccurence(String s) {        
        for (int i = 0; i < s.length(); i++) {
            // Don't include white spaces -         
            if (Character.isWhitespace(s.charAt(i))) {
                continue;
            } else {
                str.put(i, s.charAt(i));
                charList.add(s.charAt(i));
            }
        }
        for(Character val:str.values()){
                getCount(val);
        }
    }

    static boolean flag = false;
    public static int getCount(Character c) {
        int ct = 0;
        for (int i = 0; i < charList.size(); i++) {         
            c = charList.get(i);
            if (charList.contains(c)) {
                ct++;
                flag=false;
            }           
        }
        if(flag==false)
        {
        System.out.println(c + ":" + ct);
        }
        return ct;
    }
}

这是我得到的输出:

Please enter a string
my name is stack overflow
w:21
w:21 
w:21 
w:21 
w:21 
w:21 
w:21 
w:21 
w:21 
w:21 
w:21 
w:21 
w:21 
w:21 
w:21 
w:21 
w:21 
w:21 
w:21 
w:21 
w:21

4 个答案:

答案 0 :(得分:1)

Guava中:

Multiset<Character> occurrences = HashMultiset.create(
    Lists.charactersOf(CharMatcher.WHITESPACE.removeFrom("the string")));

答案 1 :(得分:0)

如果你想获得字符数,那么如果你做一个Map&lt;字符,整数&gt;而不是Map&lt;整数,性质及GT;

Map<Character, Integer> charCount = new HashMap<Character,Integer>();
for (int i = 0; i < s.length(); i++) {
    Integer count = charCount.get(s.charAt(i));
    if (count == null)
        count = 0;
    charCount.put(s.charAt(i), ++count);
}

这将为您提供所有角色的地图,以及它们出现的次数。

答案 2 :(得分:0)

使用您的代码进行最小程度的更改来实现此功能:

public static int getCount(Character c) {
    int ct = 0;
    for (int i = 0; i < charList.size(); i++) {         
        //c = charList.get(i); //why were you doing this, this method takes the desired c as an input?
        if (charList.get(i).equals(c)) { //now it only incriments when that particular entry equals c, not if ANY entry matches c
            ct++;
            flag=false;
        }           
    }
    if(flag==false)
    {
    System.out.println(c + ":" + ct);
    }
    return ct;
}

在你的getCount版本中,你会遍历整个字符串,如果字母在字符串中的任何地方,每次循环都会添加一个字符串,你也可以在getCount中更改c,我认为你不应该这样做。

此更改不会使您的代码完美,重复的字母会导致重复(但正确)输出,但它应该指向正确的方向

答案 3 :(得分:-2)

而不是询问专家,我建议首先尝试通过在步骤模式下在eclipse中调试代码来自己发现错误。您可以单步执行每个语句,并在逐步执行时查看变量的值。 这很有趣,内容丰富:)