当我尝试将String数组值存储到哈希图中时,它将显示null值。请提供任何解决方案。谢谢
String temp;
String[] line=new String[15];
Map<String, String> map=new HashMap<String, String>();
InputStream stream=new FileInputStream(path);
BufferedReader reader =new BufferedReader(new InputStreamReader(stream));
while((temp = reader.readLine()) != null)
{
for(int j=0; j<line.length ;j++)
{
line[j]=reader.readLine();
String fname=line[0];
String lname=line[1];
String mobile=line[2];
String gender=line[3];
//Store array string to map
map.put("fname",fname);
//retrive map object
System.out.println(map.get(fname));
System.out.println(fname);//showing null value in console
System.out.println(lname);//array shows values
System.out.println(mobile);//array shows values
}
答案 0 :(得分:0)
您试图按值(变量fname)获取地图值
System.out.println("map.get(fname) = "+map.get(fname));
但是您应该使用map.get(key)
,并且密钥是“ fname”(作为字符串)而不是fname(作为变量)
解决方案是
System.out.println("map.get(fname) = "+map.get("fname"));
,对于FileInputStream,请尝试使用文件而不是路径:
File file = new File("C:/*****");
InputStream stream=new FileInputStream(file);