StreamTokenizer to Hashmap in Java 输出错误



我试图将每个单词作为标记存储到哈希图中。但是,有时它存储每个单词,有时它连续存储几个单词,有时甚至不存储字符。

public static void main(String[] args) {
try {
File file = new File("jarg2912.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StreamTokenizer sT = new StreamTokenizer(bufferedReader);
sT.eolIsSignificant(true);
sT.whitespaceChars(' ', 0);
HashMap<String, Object> hashMap = new HashMap<String, Object>();
while(sT.nextToken() != StreamTokenizer.TT_EOF) {
if (sT.ttype == StreamTokenizer.TT_NUMBER) {
hashMap.put(String.valueOf(sT.nval), sT);
} else {
hashMap.put(sT.sval, sT);
}
}
fileReader.close();
System.out.println("Index:");
for (String key : hashMap.keySet()) {
System.out.println(key);
}
} catch (IOException e) {
e.printStackTrace();
}
}

电流输出:

Plea
s own typed input.  Compare
Member
s bitmapped terminal the words "THE BAG" in
tenure
versa
readable
as a qualifier.  "When is the system coming up?"
natures
indirect
Sun.
goes
behaviors
t.  The result is gossipy, funny,
datagram
idiosyncrasies
posed
reader.
general.
s last {{ITS}} machines, the one on the upper
obtusity
chances
crosstalk
rods
herself
potentially
but....
annoyance.
database-theory
Haven
covering
instances
Generic
prosyletic
Editing
computer-science
weakly
tune
cam
stampe
iterating.
aware
can
numerical
eXchange
aficionados.
award
stoppage
TM-postfix
23.0
mega-
car
floating
cat
.  Reports from {old fart}s are consistent that
flew
alarm
behavior.
stamps
depersonalization
carried
cleaning
Fnord.
Suns
Morse-code
motion
closed.
BAD
has been adopted, retaining the DDT abbreviation.
s surroundings again
998.0
heavy-metal
apostrophes
distracted
Dick
poseurs
clothes
fragment
carrier
BAR
carries
response
independently
TENEX.

我需要能够将每个符号、数字和单词存储为令牌,但我不确定为什么它不起作用。

文件的一部分:

========

== 这是行话文件,版本 2.9.12,1993 年 5 月 10 日 =

=========x 这是行话文件,黑客俚语的综合纲要 阐明了黑客传统、民间传说和幽默的许多方面。

本文档(行话文件(属于公有领域,可自由使用 使用、共享和修改。 (故意(没有合法的 限制你可以用它做什么,但有关于 它的正确使用,许多黑客都非常依恋。 当您引用文件时,请提供适当引用的礼貌, 理想情况下带有版本号,因为它会随着时间的推移而变化和增长。 (适当的引用形式示例:"行话文件 2.9.12"或 "在线黑客行话文件,版本 2.9.12,1993 年 5 月 10 日"。

行话文件是黑客文化的共同遗产。 多年来,许多人自愿参加了大量志愿服务 时间维护文件并得到全网的认可 作为它的编辑。 编辑职责包括:整理 他人的贡献和建议;寻求佐证 信息;交叉引用相关条目;将文件保存在 格式一致;以及宣布和分发更新版本 周期性地。 目前的志愿编辑包括:

sT.whitespaceChars(' ', 0)更改为sT.whitespaceChars(0, ' '),将sT.eolIsSignificant(true)更改为sT.eolIsSignificant(false)

此外,您应该使用HashSet,而不是HashMap。

最新更新