gpt4 book ai didi

java - 如何将文本文件放入Java中的哈希表中?

转载 作者:行者123 更新时间:2023-12-02 04:51:04 25 4
gpt4 key购买 nike

我有一个文本文件,我想读取它并将其放入我的哈希表中。然后打印它。

我写了一段代码,我做错了什么?

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com