当我尝试将String数组值存储到hashmap时,它将显示null value.please提供任何解决方案。谢谢提前
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
}
您试图按值获取地图值(变量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而不是path:
File file = new File("C:/*****");
InputStream stream=new FileInputStream(file);