在Java中计算字符数组中的字母出现次数

时间:2014-06-04 15:54:49

标签: java arrays sorting char

我有一个项目要求我计算Java中字母的出现,并且出于某种原因,程序的输出只会让我出现字母" a"。这是一个我确实理解如何修复的问题。

public String convert(String U) {

  int aCount = 0;
  int bCount = 0;
  int c = 0;
  int d = 0;
  int e = 0;
  int f = 0;
  int g = 0;
  // ...
  // removed for brevity

  char[] count = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

  char counting = U.charAt(i);

  for (i = 0; i > U.length(); ++i) {

    if (counting == count[0]) 
      aCount = aCount + 1;    

    if (counting == count[1]) 
       bCount = bCount + 1;

    if(counting == count[2]) 
      c++;

    if(counting == count[3]) 
      d++;

    if(counting == count[4]) 
      e++;

    if(counting == count[5]) 
      f++;

    if(counting == count[6]) 
      g++;

    // removed for brevity.
    // ...

    System.out.println(aCount + bCount + c + d + e + f + g + h + i + j + k + l + m + n + o + p + q + r + s + t + u + v + w + x + y + z);
  }

  return"";
}

4 个答案:

答案 0 :(得分:1)

每次循环迭代时都没有得到一个新字符,并且每次迭代循环都要更新i变量的计数器。它应该是一个不同的迭代索引变量

// not here. INSIDE the loop
// char counting = U.charAt(i)
for( int index = 0; index < U.length(); ++index )
{
    char counting = U.charAt(index)
    if( counting == count[0] ) ++a;
    else if( counting == count[1] ) ++b;
    else if( counting == count[2] ) ++c;
    // ...
}

或者你可以使用foreach循环,它更简洁,使意图更清晰。

for( char counting : U.toCharArray() )
{
    if( counting == count[0] ) ++a;
    else if( counting == count[1] ) ++b;
    else if( counting == count[2] ) ++c; 
    // ...
}

答案 1 :(得分:0)

你总是在counting位置获得i字符。好吧,i是一个int(计算出来的字符&#39; i&#39;,在你的&#34中定义;为了简洁而删除&#34;部分,我猜),初始化为0,所以你总是得到U的第一个字符......我相信你的IDE已经警告过你......

counting = ...置于循环中并将for (int i = ...)更改为for (int loopCounter = ...)应解决此问题:

for (int loopCounter = 0; loopCounter < U.length; loopCounter++) {
    char counting = U.charAt(loopCounter);
    ...
}

答案 2 :(得分:0)

这行已经放在迭代U字符串

的外部循环中
char counting = U.charAt(i);

这就是您只获得A's count

的原因

将其保持在循环内

并且还使用if else而不是if always。

所以它应该像下面的

for (i = 0; i < U.length(); ++i) 
{
char counting = U.charAt(i);

  if (counting == count[0]) 
  {
    aCount = aCount + 1;    
  } 
  else if (counting == count[1]) 
    {
     bCount = bCount + 1;
    }
  else if(counting == count[2]) 
    {
        c++;
    }
// and so on

}

答案 3 :(得分:0)

示例程序实际打印总和或全部小写字符出现。如果这是要求那么它可以简单地完成(比如String由str表示)

int charCountSum = str.replaceAll("[^a-z]", "").length();

但是如果你想要个别角色出现小写a-z那么

Map<Character, Integer> occuranceCounter = new HashMap<Character, Integer>();
int count;
for(Character c : str.replaceAll("[^a-z]", "").toCharArray()){
    count = 0;
    if(occuranceCounter.containsKey(c)){
        count = occuranceCounter.get(c);
    }
    occuranceCounter.put(c, ++count);
};

for(char c : "abcdefghijklmnopqrstuvwxyz".toCharArray()){
    int times = occuranceCounter.containsKey(c) ? occuranceCounter.get(c) : 0;
    System.out.println(c + " occurs " + times + " times.");
}