Struggling to see why this won't work. It seems to return the correct numbers. Any help please. Thanks
package com.codegym.task.task08.task0815;
import java.util.*;
/*
Census
*/
public class Solution {
public static HashMap<String, String> createMap() {
HashMap<String,String> hashMap = new HashMap<String, String>();
hashMap.put("M","Rose");
hashMap.put("G","Rose");
hashMap.put("A","Rose");
hashMap.put("J","Daffodil");
hashMap.put("N","Daffodil");
hashMap.put("K","Daffodil");
hashMap.put("L","Daffodil");
hashMap.put("Z","Klopp");
hashMap.put("X","Shearer");
hashMap.put("P","Townsend");
return hashMap;
}
public static int getSameFirstNameCount(HashMap<String, String> map, String name) {
//write your code here
//step through each element in the HashMap
//check the first name (key) of each pair and count and return the repeats
int firstNameCount = 0;
for (Map.Entry<String,String> entry : map.entrySet())
if (name.equals(entry.getKey())) firstNameCount++;
return firstNameCount;
}
public static int getSameLastNameCount(HashMap<String, String> map, String lastName) {
//write your code here
//step through each element in the HashMap
//check the last name (value) of each pair and count and return the repeats
int lastNameCount = 0;
for (Map.Entry<String,String> entry : map.entrySet())
if (lastName.equals(entry.getValue())) lastNameCount++;
return lastNameCount;
}
public static void main(String[] args) {
// System.out.println(getSameFirstNameCount(createMap(),"A"));
// System.out.println(getSameLastNameCount(createMap(),"Rose"));
}
}