*q3: Write a public static method named q3 that takes no parameters and has retu
ID: 3707942 • Letter: #
Question
*q3: Write a public static method named q3 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, opposed, pure,glad" 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, pure" containing only these two columns from "properties.csv" and only for lines with a name of "refuse", "being", "poll", or "essential"Explanation / Answer
public static void q3() { try { Scanner fin = new Scanner(new File("properties.csv")); PrintWriter pw = new PrintWriter("output.csv"); String line, name, pure; while (fin.hasNextLine()) { line = fin.nextLine(); name = line.split(",")[0]; pure = line.split(",")[2]; if(name.equals("refuse") || name.equals("being") || name.equals("poll") || name.equals("essential")) { pw.println(name + "," + pure); } } 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.