各位高手帮小弟编个程序啊         统计输入的字符串中字母的频率
          
从键盘上输入一个字符串,该字符串全部由英语字母组成,长度不超过30个字符,以“%”字符作为输入结束的标志(字符“#”不作为字符串的字符)。要求程序能够完成:
1.输入时若输入了字母或“#”以外的字符,应给出出错信息,重新输入。
2.统计输入字符串中没个字母出现的次数,并按次数值由大到小的顺序输出结果.

解决方案 »

  1.   

    我想给你这些你应该会知道怎么写了吧!
    public class NJTextField extends JTextField {
      private int thisLen = 0;
      private int maxLen = Integer.MAX_VALUE;  public NJTextField() {
        super();
      }  public NJTextField(int cols, int maxLen) {
        super(cols);
        this.maxLen = maxLen;
      }
      public NJTextField(int maxLen) {
        super(0);
        this.maxLen = maxLen;
      }
      protected Document createDefaultModel() {
        return new LengthLimitedDocument();
      }
      class LengthLimitedDocument extends PlainDocument {
        public void insertString(int offs, String str, AttributeSet a) throws
            BadLocationException {
          if (str == null || thisLen >= maxLen)
            return;
          //控制输入的字符是数字即小于等于‘9’大于等于‘0’
          if(str.toCharArray()[str.length()-1]>='0' && str.toCharArray()[str.length()-1]<='9'){
            if (thisLen + str.length() > maxLen)//如果是最大数将不写入Textfield
              str = str.substring(0,maxLen - thisLen);
            thisLen += str.length();
            super.insertString(offs, str, a);
          }    }
        public void remove(int offs, int len) throws BadLocationException {
          thisLen -= len;
          super.remove(offs, len);
        }
      }
    }
    我这里写的主要是为了控制这个TEXTFIELD不可以输入字符串。我知道的就这些,你自己要的功能你自己改吧。分少了点啊。
      

  2.   

    参考一下:
    public class ReaderChar {    public static void main(String[] args) throws IOException {
            InputStream in = System.in;
            System.out.println("Input a string:  end of '%'");
            String s = "abcdefghijklmnopqrstuvwxyz";
            char c = (char) in.read();
            Map m = new TreeMap();
            while (c != '%') {
                for (int i = 0; i < s.length(); i++) {                Character ch = new Character(c);
                    if (c == s.charAt(i)) {
                        if (m.containsKey(ch))
                            ((Counter) m.get(ch)).increment();
                        else
                            m.put(ch, new Counter());
                    }
                }
                c = (char) in.read();
            }        System.out.println(m);
          
        }
    }class Counter{
        int i = 1;    public String toString() {
            return Integer.toString(i);
        }    public void increment() {
            i++;
        }     }
      

  3.   

    能否用applet写给完整java的程序啊