求救,请帮我看一下,出了什么问题

zl1981 2002-12-17 11:37:33
我的程序运行可以,但是得不到我想要的结果。需要可以输入一段文字,然后数一下里面有多少个字符(character),还有多少个单词( words),有多少行(line),每个字符出现几次。
代码在下面:

import java.io.*;
import java.util.StringTokenizer;

public class InputTest
{
public static void main (String[] args)
throws IOException
{
BufferedReader console= new BufferedReader (
new InputStreamReader(System.in));

System.out.println ( " Enter your text, please( In Dos you type Ctrl+c, on UNIX you type Ctrl+D to finish your text): " + " \n" );
boolean done= false;
do
{
String inputLine = console.readLine();
if ( inputLine==null)
{
done = true;
System.out.println ( " Error, can not read the text.");
}
else
{
// count the number of words and characters
int characters=0;
StringTokenizer tokenizer= new StringTokenizer(inputLine);
int words= tokenizer.countTokens();
String[] data= new String[words];
for ( int i=0; i<data.length; i++)
{
data[i]= tokenizer.nextToken();
characters+= data[i].length();
}

// count the number of lines
int count=0;
String line= console.readLine();
do
{
count++;
line=console.readLine();
}
while ( line != null );

String output="\n" + " This text has " + count + " lines," + words + " words," + characters + " characters.";
System.out.println ( output );

// Count the occrrences of letters
String LowerCase= inputLine.toLowerCase();
LowerCase= LowerCase.trim ();
char letter[]= { "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´};
String l_count="";
for ( int l=0; l<=25; l++)
{
int start = 0, n=0;
int end=LowerCase.length();

do {
char character= LowerCase.charAt(start);
if ( character== letter[l])
{
n++;
}
start++;
} while ( start< end);

System.out.println ( "Letter " + letter[l] + " : " + l_count );
}

}
} while ( !done);
}

}

谢谢各位大侠了!!!!

...全文
82 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
qxjavajava 2002-12-17
  • 打赏
  • 举报
回复
你的StringTokenizer tokenizer= new StringTokenizer(inputLine); 有问题。所以导致你不能正确的分析字符串。
public StringTokenizer(String str)Constructs a string tokenizer for the specified string. The tokenizer uses the default delimiter set, which is " \t\n\r\f": the space character, the tab character, the newline character, the carriage-return character, and the form-feed character. Delimiter characters themselves will not be treated as tokens.
--------------用StringTokenizer()这个构造函数可以用来分析行数
用StringTokenizer(inputLine," "); 来分析word的数目
cnlzy 2002-12-17
  • 打赏
  • 举报
回复
我只是纠正了分析行数和字符出现次数的处理。
至于多少个单词的问题自己搞定吧,不动脑不动手是没办法进步噢!

import java.io.*;
import java.util.StringTokenizer;

public class InputTest
{
public static void main (String[] args) throws IOException
{
BufferedReader console= new BufferedReader (
new InputStreamReader(System.in));

System.out.println ( " Enter your text, please( In Dos you type Ctrl+c, on UNIX you type Ctrl+D to finish your text): " + " \n" );
boolean done= false;
do
{
String inputLine = console.readLine();
if ( inputLine==null)
{
done = true;
System.out.println ( " Error, can not read the text.");
}
else
{
StringBuffer sbInputString = new StringBuffer("");
// count the number of words and characters
int characters=0;
StringTokenizer tokenizer= new StringTokenizer(inputLine);
int words= tokenizer.countTokens();
String[] data= new String[words];
for ( int i=0; i<data.length; i++)
{
data[i]= tokenizer.nextToken();
characters+= data[i].length();
}

// count the number of lines
int count=0;
String line= inputLine;//console.readLine();
do
{
count++;
sbInputString.append(line);
line=console.readLine();

}
while ( line != null );


String output="\n" + " This text has " + count + " lines," + words + " words," + characters + " characters.";
System.out.println ( output );
// Count the occrrences of letters
String LowerCase= sbInputString.toString().toLowerCase();
LowerCase= LowerCase.trim ();
char letter[]= { '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'};
String l_count="";
for ( int l=0; l<=25; l++)
{
int start = 0, n=0;
int end=LowerCase.length();

do {
char character= LowerCase.charAt(start);
if ( character== letter[l])
{
n++;
}
start++;
} while ( start< end);

System.out.println ( "Letter " + letter[l] + " : " + n );
}

}
} while ( !done);
}

}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧