How do you add elements to a hashmap?

33,245

It gives you a null value because when you use get(), the key being passed as a paramanter (fVal) is a String, whereas in the Hashmap has its key defined as an Integer.

To solve this issue, have the fVal variable be an Integer that gets the nextInt()

int fVal = sc.nextInt();

You did this when soliciting data-entry, "What would the key to be?". Do the same when prompting for search criterion, "What value would you like to find?", and when prompting for deletion, "Would you like to remove a key?".

Share:
33,245
Jghorton14
Author by

Jghorton14

We are all just scripts running in the background. I like to code and take naps.

Updated on August 26, 2020

Comments

  • Jghorton14
    Jghorton14 over 3 years

    I am new to HashMaps. I was wondering if I could add ask the user what he or she wanted the key to be and what they wanted the value to be. So far I am getting the HashMap to print out but for some reason when I ask for the view the value it returns null and when I try to remove the key it also doesn't remove it.

    import java.util.Scanner;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Set;
    
    public class HashMapDemo {
      public static void main (String [] args){
        //declaring a hashmap   
        HashMap<Integer, String> hm = new HashMap<Integer, String>();
    
        //adding elements to the hashmap
        hm.put(1, "Josh");
        hm.put(2, "Max");
        hm.put(3, "Karan");
    
        //declaring the Scanner
        Scanner sc = new Scanner(System.in);
        System.out.println("Would you like to add Elements to the hashmap?");
        String scYN = sc.nextLine();
        scYN = scYN.toLowerCase();
        if(scYN.equals("yes")) {
            System.out.println("What would the key to be?");
            int key = sc.nextInt();
            sc.nextLine();
            System.out.println("What would the value to be?");
            String val = sc.nextLine();
            hm.put(key, val);           
        }else if (scYN.equals("no")) {
            System.out.println("False");
        }else{
            System.out.println("False");
        }
    
        //displaying content 
        Set set = hm.entrySet();
        Iterator iterator = set.iterator();
        while(iterator.hasNext()){
            Map.Entry mentry = (Map.Entry)iterator.next();
            System.out.print("Key is: "+ mentry.getKey() + " & Value is: ");
            System.out.println(mentry.getValue());
        }
    
        /* Get values based on key*/
    
        System.out.println("What value would you like to find?");
        String fVal = sc.nextLine();        
        String var= hm.get(fVal);
        System.out.println("Value at index " + fVal + " is: "+ var);
    
        /* Remove values based on key*/
    
        System.out.println("Would you like to remove a key?");
        String remKey = sc.nextLine();
        hm.remove(remKey);
        System.out.println("Map key and values after removal:");
        Set set2 = hm.entrySet();
        Iterator iterator2 = set2.iterator();
        while(iterator2.hasNext()) {
            Map.Entry mentry2 = (Map.Entry)iterator2.next();
            System.out.print("Key is: "+mentry2.getKey() + " & Value is: ");
            System.out.println(mentry2.getValue());
        }
    
    }
    

    }

    Thank you for your time