0
Guys help me debug this code please..
import java.util.HashMap; import java.util.Scanner; public class working { public static void main(String[]args) { HashMap<String,Integer> clients=new HashMap<String,Integer>(); clients.put("John" , 1539); clients.put("Joan",1600); clients.put("Lenny",1700); String x; System.out.println("PLEASE INPUT THE CLIENT'S NAME ..."); Scanner in= new Scanner(System.in); String x= in.newString(); System.out.println(clients.get("x")); } }
1 Resposta
+ 1
I think it's used to check for a certain key (included in the hashMap) entered and if that key is present in the hashMap it prints the value related to the key. For example if you enter John, it checks on the hashMap for that key => it's in the hashMap. So it prints the value related to the key 1539. You have to remove the double quotes in get("x") => get(x) (because x (literally) is not present in the hashMap and because we are comparing the entered String with the keys in the hashMap). Change also the line after the scanner it should be just x ( x is declared as String in one of the previous rows). Instead of creating new objects with new String, better use just x = in.nextLine();