Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的文件不是';我的java程序无法读取_Java_Map - Fatal编程技术网

我的文件不是';我的java程序无法读取

我的文件不是';我的java程序无法读取,java,map,Java,Map,我有一些Java代码如下: Map<Map<String,String>,String> map = new HashMap<>(); int i = 0; try (BufferedReader br = new BufferedReader(new FileReader("properties.txt"))) { String sCurrentLine; while ((sCurrentLine = br.readLine

我有一些Java代码如下:

Map<Map<String,String>,String> map = new HashMap<>();
int i = 0;
try (BufferedReader br = new BufferedReader(new FileReader("properties.txt")))
{

        String sCurrentLine;
        while ((sCurrentLine = br.readLine()) != null) {
                i++;
                String[] parts = sCurrentLine.split(",");
                System.out.println(parts[2]);
                Map<String,String> tempMap = new HashMap<>();
                tempMap.put("issuing_bank",parts[1]);
                tempMap.put("card_switch",parts[2]);
                tempMap.put("card_Type",parts[3]);
                map.put(tempMap,parts[0]);
        }

} catch (IOException e) {
        e.printStackTrace();
}
我非常困惑,为什么只有
12个
元素被读入我的
map
。我是不是遗漏了什么


提前感谢。

这是因为您使用了一个映射(tempMap)作为键。
这是错误的

这是因为您使用了一个映射(tempMap)作为键。
这是错误的

解决方案很简单:这一行中的参数顺序错误:

map.put(tempMap,parts[0]);
应该说

map.put(parts[0],tempMap);
必须相应地更改变量声明的类型参数。你在哪里

Map<Map<String,String>,String> map = new HashMap<>();
首先,这种方法更好,因为您的阅读循环变得更简单、更切题:

while ((sCurrentLine = br.readLine()) != null) {
  final CardHolder ch = new CardHolder(sCurrentLine.split(","));
  map.put(ch.cardNumber, ch);
}
此外,这将允许您更好地控制记录的其他方面;例如,一个很好的自定义
toString
和类似的。还要注意的是,这几乎没有产生更多的代码:它只是按照关注点分离原则进行了重组


(最后一个小观察:在Java中,字符串变量的s前缀不是常见的,因为它在静态类型语言中是多余的;请放心,在Java中,您永远不会遇到错误,因为预期字符串所在的位置出现了整数。)

解决方案很简单:这一行中的参数顺序错误:

map.put(tempMap,parts[0]);
应该说

map.put(parts[0],tempMap);
必须相应地更改变量声明的类型参数。你在哪里

Map<Map<String,String>,String> map = new HashMap<>();
首先,这种方法更好,因为您的阅读循环变得更简单、更切题:

while ((sCurrentLine = br.readLine()) != null) {
  final CardHolder ch = new CardHolder(sCurrentLine.split(","));
  map.put(ch.cardNumber, ch);
}
此外,这将允许您更好地控制记录的其他方面;例如,一个很好的自定义
toString
和类似的。还要注意的是,这几乎没有产生更多的代码:它只是按照关注点分离原则进行了重组


(最后还有一个小观察:在Java中,字符串变量的s前缀并不常见,因为它在静态类型语言中是多余的;请放心,在Java中,您永远不会遇到错误,因为在预期字符串的位置会出现整数。)

我想您在

map.put(tempMap,parts[0]);
颠倒过来。您正在将tempMap映射到数字,您可能希望将数字映射到tempMap:

map.put(parts[0], tempMap);
使用

Map Map=newhashmap();

我想你的论点是正确的

map.put(tempMap,parts[0]);
颠倒过来。您正在将tempMap映射到数字,您可能希望将数字映射到tempMap:

map.put(parts[0], tempMap);
使用

Map Map=newhashmap();

您应该使用Map作为值,因为HashMap相等性是基于它拥有的键值对。

map.put(parts[0],tempMap);
下面一个简单的程序将说明这一事实

    Map<String, String> tempMap1 = new HashMap<String, String>();
    tempMap1.put("issuing_bank", "ICICI");
    tempMap1.put("card_switch", "Visa");
    tempMap1.put("card_Type", "Debit");

    Map<String, String> tempMap2 = new HashMap<String, String>();

    tempMap2.put("issuing_bank", "ICICI");
    tempMap2.put("card_switch", "Visa");
    tempMap2.put("card_Type", "Debit");

    System.out.println(tempMap1.equals(tempMap2));//Prints true
所以你的声明如下。请记住,在HashMap中应该始终使用不可变对象作为键

Map<String,Map<String,String>> map = new HashMap<String,Map<String,String>>();
Map Map=newhashmap();

您应该使用Map作为值,因为HashMap相等性是基于它拥有的键值对。

map.put(parts[0],tempMap);
下面一个简单的程序将说明这一事实

    Map<String, String> tempMap1 = new HashMap<String, String>();
    tempMap1.put("issuing_bank", "ICICI");
    tempMap1.put("card_switch", "Visa");
    tempMap1.put("card_Type", "Debit");

    Map<String, String> tempMap2 = new HashMap<String, String>();

    tempMap2.put("issuing_bank", "ICICI");
    tempMap2.put("card_switch", "Visa");
    tempMap2.put("card_Type", "Debit");

    System.out.println(tempMap1.equals(tempMap2));//Prints true
所以你的声明如下。请记住,在HashMap中应该始终使用不可变对象作为键

Map<String,Map<String,String>> map = new HashMap<String,Map<String,String>>();
Map Map=newhashmap();

您应该更改创建地图的方式。。代替您的以下声明:-

Map<Map<String,String>,String> map = new HashMap<>();
致:

map.put(parts[0], tempMap);

您应该更改创建地图的方式。。代替您的以下声明:-

Map<Map<String,String>,String> map = new HashMap<>();
致:

map.put(parts[0], tempMap);

newfilereader(“properties.txt”)
文件可能会成为一个文件。另请参见。
newfilereader(“properties.txt”)
文件可能会成为一个文件。另见,谢谢!我没注意到!谢谢我没注意到!