1. Write a public static method named q1 that takes no parameters and has return
ID: 3711909 • Letter: 1
Question
1. Write a public static method named q1 that takes no parameters and has return type HashMap of String to String. In this method, you may assume there is a file named "monthly.csv" with lines in the format "spread(String),commonly(String),more(String),early(String),hence(String),sanction(String),surely(String),expense(Integer)" where each Integer column contains only well-formed integers. There is no header line in this file. This method will return a new HashMap that maps "more" to "sanction" by putting one key-value pair into the HashMap for each line in "monthly.csv".
Explanation / Answer
public static HashMap q1() { try { Scanner fin = new Scanner(new File("monthly.csv")); String line, more, sanction; HashMap map = new HashMap(); while (fin.hasNextLine()) { line = fin.nextLine(); more = line.split(",")[2]; sanction = line.split(",")[5]; if(!map.containsKey(more)) { map.put(more, sanction); } } fin.close(); return map; } catch (FileNotFoundException e) { e.printStackTrace(); return null; } }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.