k ok q4: Write a public static method named q4 that takes no parameters and has
ID: 3712563 • Letter: K
Question
k ok q4: Write a public static method named q4 that takes no parameters and has return type void. In this method, you may assume there is a file named "properties.csv" with lines in the format "name,gentle,straw" where "name" is a String and all other values are well-formed integers. There is no header line in this file. This method will create a new file named "output.csv" in the format "name, straw" containing only these two columns from "properties.csv" and only for lines with a name of "developing", "worry", "gold", or "spell" 18 18Explanation / Answer
public static void q4() { try { Scanner fin = new Scanner(new File("properties.csv")); PrintWriter pw = new PrintWriter("output.csv"); String line, name, straw; while (fin.hasNextLine()) { line = fin.nextLine(); name = line.split(",")[0]; straw = line.split(",")[2]; if(name.equals("developing") || name.equals("worry") || name.equals("gold") || name.equals("spell")) { pw.println(name + "," + straw); } } fin.close(); pw.flush(); pw.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.