*q4: Write a public static method named q4 that takes no parameters and has retu
ID: 3709035 • Letter: #
Question
*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 , associate,emerging, collective , nice" 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,nice" containing only these two columns from "properties.csv" and only for lines with a name of "motivation", *"orange", or "happiness"Explanation / Answer
public static void q4() { try { Scanner fin = new Scanner(new File("properties.csv")); PrintWriter pw = new PrintWriter("output.csv"); String line, name, nice; while (fin.hasNextLine()) { line = fin.nextLine(); name = line.split(",")[0]; nice = line.split(",")[4]; if(name.equals("motivation") || name.equals("orange") || name.equals("happiness")) { pw.println(name + "," + nice); } } 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.