我有一个文本文件,我想读取它并将其放入我的哈希表中。 然后打印它。
我写了一段代码,我做错了什么?
public static void main(String[] args) throws FileNotFoundException, IOException {
Hashtable< Integer, String > hash = new Hashtable< Integer, String >();
BufferedReader rd = new BufferedReader( new FileReader ("students.txt"));
String line = "";
int i = 0;
while (line != null){
line = rd.readLine();
hash.put(i, line);
i++;
}
for ( int j = 0 ; j < hash.size() ; j++){
System.out.println(hash.get(j));
}
}
最佳答案
代码看起来不错。修正以下一个错误
BufferedReader br = new BufferedReader(new FileReader ("students.txt"));
while ((thisLine = br.readLine()) != null) {
System.out.println(thisLine);
}
关于java - 如何将文本文件放入Java中的哈希表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29237073/