【问题标题】:toString method letter counter classtoString 方法字母计数器类
【发布时间】:2016-03-01 02:19:05
【问题描述】:

所以我有这个方法来计算一个句子中的字母并将它们放入一个大小为 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
}

他不希望我们打印出没有出现在字符串中的字符。他的驱动程序中的字符串看起来像“Aa678 ,.. zZ”;在我的测试仪中,它只打印出 0 0 0... 26 次,而它应该打印出 A's = 2 和 Z'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

【问题讨论】:

  • 这里是一些代码:for(int i=0;i&lt;26;i++){ if(count[i]&gt;0) a.append((char)(65+i) + " " + count[i]); }
  • 它仍然打印出Needs more work 还有其他建议吗? @奈雪
  • 代码如何打印出Needs more work??
  • 他的测试程序。你想看吗@Nayuki

标签: java arrays char


【解决方案1】:

希望对你有帮助。

String upper = "Aa678 ,.. zZ.qammar".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 = 4,M = 2,Q = 1,R = 1,Z = 2,

【讨论】:

  • 感谢您的帮助!
【解决方案2】:

这应该适合你:

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

希望对你有所帮助。

【讨论】:

  • 这仍然不起作用......我将在我的问题中提供我的完整课程/测试人员
  • 当我从测试程序中用双 System.out.println(lc); 打印出来时,它会打印出 a's = 2 z's = 2 然后打印出 a's = 4, z's = 4。我的老师告诉我要使用某种明确的方法,我认为是zeroArray() 方法,但我不知道该声明在哪里
  • 把这个放在你的countLetters()方法的开头count = new int[26];
  • 嘿,我确实让它工作了,但谢谢你的建议。我只需要使用zeroArray() 方法清除数组
  • 你认为你能帮我处理我的另一个问题吗,我在另一个问题中问过
猜你喜欢
  • 1970-01-01
  • 2021-12-19
  • 2012-01-11
  • 2017-05-25
  • 1970-01-01
  • 1970-01-01
  • 2016-12-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多