
What's problem with this code? why output is not displaying? how can i solve it? Thanks,
package com; import java.util.HashMap;
public class BookDemo { HashMap<String, Integer>hm=new HashMap<String, Integer>(); public static void main(String[] args) { BookDemo bm=new BookDemo(); bm.add(); } public void add() { hm.put("JAVA", 350); hm.put("C", 260); hm.put("HTML", 200); hm.put("Visual Basic", 400); hm.put("Microsoft office",300); } public int price(String st) { return hm.get(st); } }

Hello Friend,
Neither you have called the method price() nor you have print anything.Anyways, we have modified your code:
import java.util.*;
public class BookDemo {
static HashMap<String, Integer>hm=new HashMap<String, Integer>();
public static void main(String[] args) {
BookDemo bm=new BookDemo();
bm.add();
Set set = hm.entrySet();
Iterator i = set.iterator();
while(i.hasNext()){
Map.Entry me = (Map.Entry)i.next();
System.out.println(price(me.getKey().toString()));
}
}
public void add() {
hm.put("JAVA", 350);
hm.put("C", 260);
hm.put("HTML", 200);
hm.put("Visual Basic", 400);
hm.put("Microsoft office",300);
}
public static int price(String st) {
return hm.get(st);
}
}
Thanks
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.