问题描述
importjava.util.*;publicclassInPut{publicstaticvoidmain(String[]args){Scannersc=newScanner(System.in);ArrayList<HashMap>al=newArrayList<HashMap>();for(;;){System.out.println("请输入商品信息:");Stringmess=sc.next();String[]st=mess.split(",");if(st.length!=4)continue;String[]d=st[2].split("-");Calendarc=Calendar.getInstance();c.set(Integer.parseInt(d[0]),Integer.parseInt(d[1])-1,Integer.parseInt(d[2]));Datedate=c.getTime();doubleprice=Double.parseDouble(st[3]);HashMap<String,Object>hm=newHashMap<String,Object>();hm.put("pid",st[0]);hm.put("pname",st[1]);hm.put("date",date);hm.put("price",price);al.add(hm);System.out.println("按任意键继续,按n结束:");Strings=sc.next();if(s.equalsIgnoreCase("n"));break;}ArrayList<Product>al1=newArrayList<Product>();for(inti=0;i<al.size();i++){HashMaphm=al.get(i);Objectobj1=hm.get("pid");Objectobj2=hm.get("pname");Objectobj3=hm.get("date");Objectobj4=hm.get("price");Productp=newProduct();if(obj1instanceofString){p.setPid((String)obj1);}if(obj2instanceofString){p.setPname((String)obj2);}if(obj3instanceofString){p.setDate((Date)obj3);}if(obj4instanceofDouble){p.setPrice((Double)obj4);}al1.add(p);System.out.println(p);}System.out.println("输入所要查的id");Stringid=sc.next();Productp=newProduct();p.setPid(id);if(al1.contains(p)){System.out.println("存在");}else{System.out.println("不存在");}}}下面是运行结果请输入商品信息:1,niu,2009-2-3,33按任意键继续,按n结束:n1:niu:null:33.0输入所要查的id第二个冒号后面的是日期显示的是null这是为什么??
解决方案
解决方案二:
if(obj3instanceofDate){p.setDate((Date)obj3);}这个应该是Date,而不是String
解决方案三:
引用1楼shixitong的回复:
if(obj3instanceofDate){p.setDate((Date)obj3);}这个应该是Date,而不是String
谢谢问题已解决无意间敲错了没改过来