package com.codegym.task.task08.task0816;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/*
Kind Emma and the summer holidays
*/
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"));
map.put("Federer", df.parse("AUGUST 8 1981"));
map.put("Lenin", df.parse("SEPTEMBER 2 1990"));
map.put("Johnson", df.parse("AUGUST 31 1890"));
map.put("Nadal", df.parse("JUNE 3 1986"));
map.put("Peterson", df.parse("JULY 15 2000"));
map.put("Clarke", df.parse("DECEMBER 1 1969"));
map.put("Lemonade", df.parse("JANUARY 31 2019"));
map.put("Mr.Snake", df.parse("APRIL 20 1960"));
map.put("VODKA", df.parse("MAY 31 2009"));
//write your code here
return map;
}
public static void removeAllSummerPeople(HashMap<String, Date> map) {
//write your code here
Calendar c = Calendar.getInstance();
Iterator<Map.Entry<String, Date>> itr = map.entrySet().iterator();
while(itr.hasNext()){
Map.Entry<String, Date> entry = itr.next();
c.setTime(entry.getValue());
if(c.get(Calendar.MONTH) == 5 || c.get(Calendar.MONTH) == 6 || c.get(Calendar.MONTH) == 7){
itr.remove();
}
}
}
public static void main(String[] args) {
HashMap<String, Date> map = new HashMap<String, Date>();
map = createMap();// value in createMap() function is not getting assigned in the HashMap variable map.
}
}
How to assign the map value in the map variable in the main method.
Under discussion
Comments (3)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
25 March 2019, 17:12
There is an unhandled exception with the createMap method because it throws a ParseException. You can either add the exception to the main method; or wrap the parse code in a try-catch block and remove the "throws ParseException" from the createMap method.
+1
Ashutosh Sharma
26 March 2019, 04:49
I want to ask one question from you Guadalupe. How can I become an expert Java programmer like you?? Is CodeGym enough for this?? Or do I really need to read some books??
0
Guadalupe Gagnon
26 March 2019, 04:58
I learned 90% of what I know through codegym. I'm not an expert though, I still use google alot to help solve things (and I suggest everyone to do so as well). Your problem wasnt that hard though, intelliJ told me what was wrong when I copied your code and pasted it in. That is another thing I can suggest, learn intelliJ and what it can do. Any time you have a red squiggly line in intelliJ, click on it and hit alt+enter for suggestions to fix the problem.
+3