我正在尝试将每个单词作为令牌存储到hashmap中。但是,它有时会存储每个单词,有时会连续存储几个单词,有时甚至不会存储字符。
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日=========
这是行话文件,一个全面的概要黑客俚语照亮了黑客传统,民间传说和幽默的许多方面。
本文档(行话文件)属于公共领域,可自由使用、共享和修改。你可以用它做什么(故意)没有法律限制,但关于它的正确使用有许多黑客强烈依附的传统。当您引用文件时,请扩展适当引用的礼貌,最好是带有版本号,因为它会随着时间的推移而变化和增长。(适当引用形式的例子:"Jargon File 2.9.12“或"The on-line hacker Jargon File,version2.9.12,1993年5月10日”)
行话文件是黑客文化的共同遗产。多年来,许多人自愿花了相当多的时间来维护该文件,并被整个网络承认为该文件的编辑。编辑职责包括:整理他人的意见和建议;寻找确凿的信息;交叉引用相关条目;保持文件格式一致;以及定期宣布和分发最新版本。目前的志愿者编辑包括:
发布于 2018-09-29 00:16:55
将sT.whitespaceChars(' ', 0)更改为sT.whitespaceChars(0, ' '),将sT.eolIsSignificant(true)更改为sT.eolIsSignificant(false)。
此外,您应该使用HashSet,而不是HashMap。
https://stackoverflow.com/questions/52558766
复制相似问题