[英]Why hashtable is empty?
我正在尝试将文本文件中的关键字存储在哈希表中,然后读取另一个带有长字符串的文本文件,将其拆分为单词,将其放入数组中,然后循环比较哈希表值与数组值是否相等。
hash 表 function
sysout h 值的 output 为:
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
我在 main 中用于将文本文件的值与哈希表中的值进行比较的函数是
public static void main(String[] args) throws IOException {
hash_table keyword = new hash_table();
String text, t, thisline;
text = "";
BufferedReader br = new BufferedReader(new FileReader("words/TextFile.txt"));
while ((thisline = br.readLine()) != null) {
if (thisline.length() > 0) {
text += " " + thisline;
}
}
String[] array = text.split("\\ ", -1);
int len = array.length;
for (int i = 0; i < len; i++) {
t = array[i];
for (Map.Entry<String, String> entry : keyword.hashtable().entrySet()) {
if (entry.getValue().equals(t)) {
System.out.println("same");
}
}
}
}
另一件事,当我改变
if (entry.getValue().equals(t)) {
和
if (!entry.getValue().equals(t)) {
它应该系统输出"same"
,但事实并非如此。
我试图修复它几个小时没有成功,希望有人能提供帮助!
替换此代码
while ((thisline = br.readLine()) != null) { // Will Iterate until br.readLine returns null
//System.out.println(thisline);
}
while (thisline != null) { // this variable is null, so, this chunk of code is never executed.
line = br.readLine();
h.put("" + i, line);
i++;
}
System.out.println(h); // retrun empty
return h;
有了这个
while ((thisline = br.readLine()) != null) {
h.put("" + i, thisline);
i++;
}
System.out.println(h);
return h;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.