toString方法字母计数器类

时间:2016-03-01 02:19:05

标签: java arrays char

所以我有这个方法来计算一个句子中的字母并将它们放入一个大小为26的int数组中:

public void countLetters()
{
 String upper = sentence.toUpperCase();
 int ascii;
 for (int k = 0; k <upper.length(); k++)
 {
    char ch = upper.charAt(k);
    if (Character.isLetter(ch))
    {
      ascii = (int) ch;
      ascii -= 65;
      count[ascii] += 1;
    }    
 }
}

count变量是我的数组,我有ASCII值和东西...... 我不知道怎么写的是toString方法 这些是Javadocs:

 /**
 *  Returns a String containing the letters in the sentence
 *  and how many times they occurred.
 *  @return returns a formatted string with only the letters
 *          that occurred in the string, each on a separate line.
 */

public String toString()
{
  StringBuffer a = new StringBuffer();
  for (Integer i : count)
  {
     a.append(i + " ");
  }
  return a.toString();
  //This is what I have so far, but I need to print them out in a format that looks like this: 
I sure hope this WORKS!!!

e's = 2
h's = 2
i's = 2
k's = 1
o's = 2
p's = 1
r's = 2
s's = 3
t's = 1
u's = 1
w's = 1
}

他不希望我们打印出不会出现在字符串中的字符。他的驱动程序中的字符串看起来像&#34; Aa678,.. zZ&#34 ;;在我的测试仪中它只打印0 0 0 ... 26次,而它应该打印出A&#39; s = 2和Z&#39; s = 2。

测试程序看起来像

public class LetterCounterDriver
{
public static void main(String[] args)
{
    String s = "Aa678 ,.. zZ";
    LetterCounter lc = new LetterCounter(s);
    System.out.println(lc);

    if (lc.toString().equals("a's = 2\nz's = 2\n"))
        System.out.println("Works");
    else if (strip(lc.toString()).equals(strip("a's = 2\nz's = 2\n")))
        System.out.println("Close to working.  Check you spacing and capitalization!");
    else
        System.out.println("Needs some more work");
}

/**
 *  Removes:
 *      space -> 32
 *      (\t) tab -> 9
 *      (\n) line feed -> 10
 *      (\f) form feed -> 12
 *      (\r) return -> 13
 */
private static String strip(String s)
{
    String remove = " \t\n\f\r";
    String x = "";
    for (int k = 0; k < s.length(); k++)
        if (remove.indexOf(s.charAt(k)) == -1)
            x += s.charAt(k);
    return x.toLowerCase();
}
}

输出说

A's = 2 Z's = 2 
Needs some more work

它应该说

Works

更新:这是完整的课程。请注意,我写了所有这些。

import java.util.Arrays;
public class LetterCounter
{
private String sentence;
private int[] count;

/**
 *  Creates a LetterCounter object
 */
public LetterCounter(String s)
{
    count = new int[26];
sentence = s;
}

/**
 *  Sets all locations in the letter count array to zero
 *  @postcondition sets all locations in the letter count array to zero
 */
public void zeroArray()
{
    for (int k = 0; k < count.length; k++)
     count[k] = 0;
}

/**
 *  Computes the array containing a count for each letter 
 *  in the sentence
 *  @postcondition computes the counter array for letters a - z
 */
private void countLetters()
{
 String upper = sentence.toUpperCase();
 int ascii;
 for (int k = 0; k <upper.length(); k++)
 {
    char ch = upper.charAt(k);
    if (Character.isLetter(ch))
    {
      ascii = (int) ch;
      ascii -= 65;
      count[ascii] += 1;
    }    
 }
}

/**
 *  Returns a String containing the letters in the sentence
 *  and how many times they occurred.
 *  @return returns a formatted string with only the letters
 *          that occurred in the string, each on a separate line.
 */
public String toString()
{
  countLetters();
  StringBuffer a = new StringBuffer();
  for (int i = 0; i < count.length; i++) 
  {
     int c = count[i];
     if (c > 0) 
     {
        char letter = (char) (i + 'a');
        a.append(letter).append("'s = ").append(c).append("\n");
     }
}
return a.toString();
}
}
//The output is 
 //a's = 2
 //z's = 2

 //Needs more work

2 个答案:

答案 0 :(得分:1)

我希望这会对你有所帮助。

String upper =&#34; Aa678,.. zZ.qammar&#34; .toUpperCase(); //你的方法然后跟随toString

public String toString(){
StringBuffer a = new StringBuffer();
for (int i = 0; i < count.length; i++)
{
   int v = count[i];
   if(v > 0){
      a.append( (char)(i + 65) + "'s = " + v + ", ");
    }
  }
 return a.toString();
}

输出: A&#39; s = 4,M&#39; s = 2,Q&#39; s = 1,R&#39; s = 1,Z&#39; s = 2,

答案 1 :(得分:0)

这应该对你有用:

public String toString() {
    StringBuffer a = new StringBuffer();
    for (int i = 0; i < count.length; i++) {
        int c = count[i];
        if (c > 0) {
            char letter = (char) (i + 'a');
            a.append(letter).append("'s = ").append(c).append("\n");
        }
    }
    return a.toString();
}

以下是与原始代码相比必须更改的内容:

  1. 我们需要使用索引进行迭代,因为我们需要索引来获取字母
  2. 当前字母是索引的值+ a的值,因为我们需要小写字母
  3. 's =部分和最后的换行符缺失
  4. 检查当前计数是否大于0
  5. 希望这会对你有所帮助。