【发布时间】:2017-01-10 00:18:16
【问题描述】:
我正在通过一本书自己练习 Java。我阅读了关于文本处理和包装类的章节,并尝试了下面的练习。
字数计数器
编写一个程序,询问用户文件名。程序应显示文件包含的单词数。
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.StringTokenizer;
public class FileWordCounter {
public static void main(String[] args) throws IOException {
// Create a Scanner object
Scanner keyboard = new Scanner(System.in);
// Ask user for filename
System.out.print("Enter the name of a file: ");
String filename = keyboard.nextLine();
// Open file for reading
File file = new File(filename);
Scanner inputFile = new Scanner(file);
int words = 0;
String word = "";
while (inputFile.hasNextLine()) {
String line = inputFile.nextLine();
System.out.println(line); // for debugging
StringTokenizer stringTokenizer = new StringTokenizer(line, " \n.!?;,()"); // Create a StringTokenizer object and use the current line contents and delimiters as parameters
while (stringTokenizer.hasMoreTokens()) { // for each line do this
word = stringTokenizer.nextToken();
System.out.println(word); // for debugging
words++;
}
System.out.println("Line contains " + words + " words");
}
// Close file
inputFile.close();
System.out.println("The file has " + words + " words.");
}
}
我从网上随机选择了这首诗来测试这个程序。我把这首诗放在一个名为 TheSniper.txt 的文件中:
Two hundred yards away he saw his head;
He raised his rifle, took quick aim and shot him.
Two hundred yards away the man dropped dead;
With bright exulting eye he turned and said,
'By Jove, I got him!'
And he was jubilant; had he not won
The meed of praise his comrades haste to pay?
He smiled; he could not see what he had done;
The dead man lay two hundred yards away.
He could not see the dead, reproachful eyes,
The youthful face which Death had not defiled
But had transfigured when he claimed his prize.
Had he seen this perhaps he had not smiled.
He could not see the woman as she wept
To the news two hundred miles away,
Or through his very dream she would have crept.
And into all his thoughts by night and day.
Two hundred yards away, and, bending o'er
A body in a trench, rough men proclaim
Sadly, that Fritz, the merry is no more.
(Or shall we call him Jack? It's all the same.)
这是我的一些输出... 出于调试目的,我打印出文件中的每一行和总字数,包括当前行中的字数。
Enter the name of a file: TheSniper.txt
Two hundred yards away he saw his head;
Two
hundred
yards
away
he
saw
his
head
Line contains 8 words
He raised his rifle, took quick aim and shot him.
He
raised
his
rifle
took
quick
aim
and
shot
him
Line contains 18 words
...
最后,我的程序显示这首诗有 176 个单词。但是,Microsoft Word 计算 174 个单词。我从打印每个单词中看到我误数了撇号和单引号。这是我的输出中出现问题的诗的最后一部分:
(Or shall we call him Jack? It's all the same.)
Or
shall
we
call
him
Jack
It
s
all
the
same
Line contains 176 words
The file has 176 words
在我的 StringTokenizer 参数列表中,当我不分隔一个看起来像撇号的单引号时,单词“It's”被计为一个。但是,当我这样做时,它被视为两个单词(It 和 s),因为看起来像单引号的撇号被分隔了。还有一句“天哪,我抓住了他!”当我不分隔单引号/撇号时被误算。在分隔它们时,撇号和单引号是同一个字符吗?我不确定如何分隔短语周围的单引号,而不是像“It's”这样的单词之间的撇号。我希望我在问我的问题时有点清楚。请要求任何澄清。任何指导表示赞赏。谢谢!
【问题讨论】:
-
有什么理由不能只使用空格(空格、制表符、换行符)作为分隔符吗?在短语
'By Jove, I got him!'中,第一个单词是'By和最后一个单词是him!'无关紧要,以便计算 单词,即使打印时看起来不太好找出找到的单词(根据您的评论,这仅用于调试)。 (另见stackoverflow.com/questions/8813779) -
谢谢!这是有道理的。
标签: java file stringtokenizer