java编程 字母数和单词数问题

来源:百度知道 编辑:UC知道 时间:2024/05/18 17:32:15
写一个java程序模块。当通过键盘输入一串字符后,报告出每个字母出现过多少次,和有多少个非字母的字符。
要求代码写的要尽可能短,不能使用字符串一遍一遍重复的方法。

改编和扩展之前的模块代码。在数出每个字母的数目后,系统需提示用户输入一个单词。然后系统报告出这个单词曾经在之前那串字符中出现过多少次。*单词不是字符的一部分,而是独立的单词,就是一串字母前后都要有空格。
这是最后一道题,截止到明天下午,作业太多,我没有时间弄了,求您帮个忙。

import java.B102`*;
class CA
{
public static void main (String [] args)
{
int ch, ndigits = 0, nletters = 0, nother = 0;
if (args.length != 1)
{
System.err.println ("usage: java CA filename");
return;
}
FileInputStream fis = null;
try
{
fis = new FileInputStream (args [0]);
while ((ch = fis.read ()) != -1)
if (Character.isLetter ((char) ch))
nletters++;
else
if (Character.isDigit ((char) ch))
ndigits++;
else
nother++;
System.out.println ("num letters = " + nletters);
System.out.println ("num digits = " + ndigits);
System.out.println ("num other = " + nother + "\r\n");
int