简体   繁体   English

尝试从文件中读取数据后创建HashMap。 获取空值。 我究竟做错了什么?

[英]Trying to create a HashMap after reading data from file. Getting null values. What am I doing wrong?

I am trying to read from a file to create a HashMap<String, HashMap<String, Integer>> . 我试图从文件中读取以创建HashMap<String, HashMap<String, Integer>>

The inputFile is in the following format: inputFile采用以下格式:

 A    B    C    D
 1   -1    2   -3
 2    3   -5    3
-3    2    7   -5
 4    3    1    1

This input file represents distances between all pairs of points. 此输入文件表示所有点对之间的距离。 Which means, it represents the following matrix: 这意味着,它代表以下矩阵:

     A    B    C    D
A    1   -1    2   -3
B    2    3   -5    3
C   -3    2    7   -5
D    4    3    1    1

While creating the map, when I print the keys and values, the correct values are printed. 在创建地图时,当我打印键和值时,会打印正确的值。 But when I try printing the map after populating it completely, it only prints the keys of the outer map correctly. 但是当我在完全填充地图后尝试打印地图时,它只能正确打印外部地图的键。 Everything else is printed as null . 其他所有内容都打印为null

What am I doing wrong? 我究竟做错了什么?

public static HashMap<String, HashMap<String, Integer>> initMap() {
    HashMap<String, HashMap<String, Integer>> distancesMap = new HashMap<String, HashMap<String, Integer>>(); 
    final String distancesFileName = "/home/name/workspace/Tester/src/main/distances.txt";
    String distancesFile = Main.readInputFile(distancesFileName);
    String[] distancesFileArray = distancesFile.split("\n");
    String[] firstLine = distancesFileArray[0].split("\t");
    for(int iii = 1; iii < distancesFileArray.length; iii++) {
        HashMap<String, Integer> tempMap = new HashMap<String, Integer>();
        String[] singleLineArray = distancesFileArray[iii].split("\t");
        for(int jjj = 0; jjj < singleLineArray.length; jjj++) {
            tempMap.put(firstLine[jjj], Integer.parseInt(singleLineArray[jjj]));
            // the following print statement (commented out) prints the correct values  
            // System.out.println(firstLine[jjj] + " " + tempMap.get(firstLine[jjj]));

        }
        distancesMap.put(firstLine[iii - 1], tempMap);
    }
    /*
     * The following lines just print the key of the outer map correctly
     * but everything else is printed as null
     */
    for(String key : distancesMap.keySet()) {
        System.out.print(key + "\t");    // prints the correct String value
        HashMap<String, Integer> tempMap = distancesMap.get(key);
        for(String innerKey : tempMap.keySet()) {
            // the following line only prints null values
            System.out.print(tempMap.get(innerKey + "\t"));
        }
        System.out.println();
    }
    return distancesMap;
}

The output I get from this is like: 我得到的输出如下:

B    nullnullnullnull
C    nullnullnullnull
D    nullnullnullnull
A    nullnullnullnull

Change 更改

System.out.print (tempMap.get (innerKey + "\t"));

to

System.out.print (tempMap.get (innerKey) + "\t");

暂无
暂无

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

相关问题 尝试将数据写入.txt文件。 我究竟做错了什么? - Trying to write the data to a .txt file. What am I doing wrong? 我正在尝试制作一个程序,将 object 保存到文件中,然后从文件中读取 object。 我究竟做错了什么? - I'm trying to make a progrm that saves an object to a file and then reads the object from the file. What am I doing wrong? 我正在尝试将一个值从 servlet 文件传递​​到 javascript 文件,但我一直为 null 我做错了什么? - I am trying to pass a value from servlet file to javascript file and i am keep getting null where am i doing wrong? 尝试使用文件输入从数组创建 object 时我做错了什么 - What am I doing wrong when trying to create an object from an array using file input 我在 MNIST 数据集中读错了什么? - What am I doing wrong reading in the MNIST data set? 我的Java程序在netbeans中运行,但不会在命令中或通过jar文件运行。 我究竟做错了什么? - My java program runs in netbeans, but will not run in command or through the jar file. What am I doing wrong? 我正在尝试读取一个简单的 Json 文件,但我保存了一个 null。 我究竟做错了什么? 我的 json 仅包含 4 个字段 - I'm trying to read a simple Json file but im getting saved a null. What am i doing wrong? My json contains only 4 fields 为什么当我尝试使用 java 从 Spark 中的 json 文件创建视图时,我得到具有 null 值的行 - why i am getting rows with null values when i am trying to create view from json file in spark with java 试图从Android上的SQLite表获取数据……我在做什么错? - Trying to get data from a SQLite table on Android… What am I doing wrong? Android在内部存储上创建文件,我在做什么错? - Android create file on internal storage, what am i doing wrong?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM