无法读取重复的字符

时间:2013-04-19 09:40:57

标签: java string loops char

我正在编写一个代码来读取字符串并计算重复的集合

public int countRepeatedCharacters()
{
    int c = 0;
    for (int i = 1; i < word.length() - 1; i++)
    {
        if (word.charAt(i) == word.charAt(i + 1)) // found a repetition
        {
            if ( word.charAt(i - 1) != word.charAt(i)) {

                c++;

            }
        }
    }     
    return c;
}

如果我尝试输入 aabbcdaaaabb 我应该有4组重复小数 aa | bb | aaaa | BB

而且我知道我没有读第一组aa因为我的索引从1开始。我尝试修复它以读取零但是然后我修复整个循环以使用更改而我失败了,是吗关于如何更改我的索引或循环的任何建议?

6 个答案:

答案 0 :(得分:1)

从我对你的问题的理解,你想要计算重复集的数量,那么这应该有所帮助。

for (int i = 0; i < word.length()-1; i++){
    if (word.charAt(i) == word.charAt(i + 1)){ // found a repetition
        if (i==0 || word.charAt(i - 1) != word.charAt(i)) {    
            c++;    
        }
    }
}   

答案 1 :(得分:1)

试试这段代码:

public int countRepeatedCharacters(String word)
{
    int c = 0;
    Character last = null;
    bool counted = false;
    for (int i = 0; i < word.length(); i++)
    {
        if (last != null && last.equals(word.charAt(i))) {  // same as previous characted
            if (!counted) {  // if not counted this character yet, count it
                c++;
                counted = true;
            }
        }
        else {      // new char, so update last and reset counted to false
            last = word.charAt(i);
            counted = false
        }
    }     
    return c;
}

编辑 - 将aaaa计为4,固定为计数为1

答案 2 :(得分:1)

试试这个----

    public int countRepeatedCharacters()
{
    int c = 0,x=0;
    boolean charMatched=false;
    for (int i = 0; i < word.length(); i++)
    {
        if(i==word.length()-1)
        {
            if (word.charAt(i-1) == word.charAt(i))
                 c++;
            break;
        }
        if (word.charAt(i) == word.charAt(i + 1)) // found a repetition
        {

            charMatched=true;
            continue;
        }
        if(charMatched==true)
        c++;
        charMatched=false;          
    }     
    return c;
}

答案 3 :(得分:1)

试试这个方法。它计算重复的特征集。

public static void main(String[] args) {
    String word = "aabbcdaaaabbc";
    int c = 0;
    for (int i = 0; i < word.length()-1; i++) {

        // found a repetition
        if (word.charAt(i) == word.charAt(i + 1)) {
            int k = 0;
            while((i + k + 1) < word.length()) {
                if(word.charAt(i+k) == word.charAt(i + k + 1)) {
                    k++;
                    continue;
                }
                else {
                    break;
                }
            }
            c++;
            i+=k-1;
        }
    }
    System.out.println(c);
}

答案 4 :(得分:0)

您可以尝试这样的事情: -

public static void main(String str[]) {
    String word = "aabbcdaaaabbc";
    int c = 1;
    for (int i = 0; i < word.length() - 1; i++) {
        if (word.charAt(i) == word.charAt(i + 1)) {
            c++;
        } else {
            System.out.println(word.charAt(i)+ " = " +c);
            c = 1;
        }
    }
    System.out.println(word.charAt(word.length()-1)+ " = " +c);
}

您可以根据需要修改此内容,方法是删除sysouts和其他内容。

答案 5 :(得分:0)

使用length()-1会导致您不考虑计算中的最后一个字符。 这会导致您丢失最后一个重复字符。

最后,我会这样做:

 public static int countRepeatedCharacters(String word)
    {
        boolean withinRepeating = false;
        int c  = 0;

        for (int i = 1; i < word.length(); i++)
        {

            if (!withinRepeating && (withinRepeating = word.charAt(i) == word.charAt(i - 1)))
            c++;                        
            else
            withinRepeating = word.charAt(i) == word.charAt(i - 1);
        }     
        return c;
    }