繁体   English   中英

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

[英]How do I put text file into a hash table in Java?

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

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

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);
         }       

我正在使用您的代码,并且已经纠正了一些错误...

我认为,此代码不正确,但他有效:)

try{
    Hashtable< Integer, String > hash = new Hashtable< Integer, String >();
    BufferedReader rd = new BufferedReader( new FileReader ("students.txt"));
    String line;

    int i = 0;
    while ((line = rd.readLine()) != null){
        hash.put(i, line);
        i++;
    }
    for ( int j = 0 ; j < hash.size() ; j++){
        System.out.println(hash.get(j));
    }
}catch(FileNotFoundException e){}catch (IOException e) {}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM