package com.codegym.task.task08.task0816;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.*;
/*
TITLE OF EXERCISE: Kind Emma and the summer holidays
INSTRUCTIONS
Create a Map<String, Date> and add ten entries that represent (last name, birth date) pairs.
Remove from the map all people born in the summer.
Hint: At CodeGym, summer lasts from June 1 to August 31.
Requirements:
The program should not display text on the screen.
The program should not read values from the keyboard.
The createMap() method must create and return a HashMap that has (String, Date) elements and contains 10 entries.
The removeAllSummerPeople() method should remove from the map all people born in the summer.
*/
public class Solution {
public static HashMap<String, Date> createMap() throws ParseException {
DateFormat df = new SimpleDateFormat("MMMMM d yyyy", Locale.ENGLISH);
HashMap<String, Date> map = new HashMap<String, Date>();
map.put("Stallone", df.parse("JUNE 1 1980"));
//write your code here
map.put("Rivermeade", df.parse("JULY 20 2004"));
map.put("Sanders", df.parse("JANUARY 5 1974"));
map.put("Smith", df.parse("FEBRUARY 12 1989"));
map.put("Rawlston", df.parse("MARCH 18 2010"));
map.put("Peters", df.parse("AUGUST 8 1975"));
map.put("James", df.parse("SEPTEMBER 25 1963"));
map.put("Jones", df.parse("JULY 1 1954"));
map.put("Lyttle", df.parse("MAY 18 1998"));
map.put("Ferdinand", df.parse("OCTOBER 11 2017"));
return map;
}
public static void removeAllSummerPeople(HashMap<String, Date> map) {
Set<Map.Entry<String, Date>> mapEntries = map.entrySet();
//get the iterator
Iterator<Map.Entry<String,Date>> mapIterator = mapEntries.iterator();
/*
create a formatter to get the "month part" of the current map entry Date
value
if <3 characters are used to specify the format, a number is returned
else a string is returned
*/
DateFormat formatTheDate = new SimpleDateFormat("MM", Locale.ENGLISH);
//iterate through the map
while(mapIterator.hasNext()){
//get the current map entry
Map.Entry<String, Date> mapEntryKey = mapIterator.next();
//get the value of the date from the current map entry
Date valueOfCurrentMapEntry = map.get(mapEntryKey);
//format valueOfCurrentMapEntry to get the "month part"
String monthOfDate = formatTheDate.format(valueOfCurrentMapEntry);
//check if the month part is equal to the numbers for June = 06, July = 07, and
//August
if(monthOfDate == "6" || monthOfDate == "7" || monthOfDate == "8"){
mapIterator.remove();
}
mapIterator.next();
}
return;
}
public static void main(String[] args) throws ParseException {
HashMap<String, Date> newMap = createMap();
removeAllSummerPeople(newMap);
/*
for(Map.Entry<String, Date> eachEntry: newMap.entrySet()) {
System.out.format("key: %s, value: %d%n", eachEntry.getKey(), eachEntry.getValue());
}
*/
}
}
hidden #10362262
Level 9
I keep getting NullPointerException. Where am I going wrong on this one? Also, it will not confirm the final condition. Thank you!
Under discussion
Comments (6)
- Popular
- New
- Old
You must be signed in to leave a comment
Ivan
14 February 2020, 15:08
Line-77 is not needed, you are in the while loop which is checking for the next value.
Line-66 should get the values, I see it is getting keys (strings).
0
hidden #10362262
14 February 2020, 15:14
Thank you for your reply. But I thought that map.get(mapEntryKey) returns the value associated with the mapEntryKey obtained on line 63.
0
Ivan
14 February 2020, 15:20
Well, i haven't touched maps in a big while now. I'll try to look into this tomorrow and dive-in.
But from what i saw in my solution is you need to get the Date (I used calendar) and get the values of the map.
+1
Misiu
14 February 2020, 17:23
Get value from EntryKey should work:
Date valueOfCurrentMapEntry = mapEntryKey.getValue();
0
Misiu
14 February 2020, 14:21
1. Months are counted from 0, so June = 05.
2. I',m not sure, but maybe it is not allowed to remove element from iterated list. I make copy of the list, iterate that one, and remove elements from original list.
0
hidden #10362262
14 February 2020, 15:33
Thank you, I did make the change of the months to be "05", "06", and "07". I also deleted the unneeded mapIterator.next() call.
I am still unable to get past the NullPointerException
0