你的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的数目
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的数目
至于多少个单词的问题自己搞定吧,不动脑不动手是没办法进步噢!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);
} }