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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the below implemented public static void q3() method. Also find the ProcessCSVFile class with q3 and main method to test the functionality of the q3 method.

Please find the below input dummy properties.csv and generated output.csv file by q3 method.

Please revert back in case anything else needs to do.

****** q3 method ******


public static void q3() {
Scanner sacnnerReader = null;
BufferedWriter bufferedWriter = null;
FileWriter fileWriter = null;
try {
File file = new File("properties.csv");
if (file.exists()) {
// creating File Writer to write CSV file
fileWriter = new FileWriter("output.csv");
// creating Buffered Writer to write CSV file
bufferedWriter = new BufferedWriter(fileWriter);
// creating sacnner Reader to read CSV file
sacnnerReader = new Scanner(file);
while (sacnnerReader.hasNextLine()) {
String line = sacnnerReader.nextLine();
String lineArray[] = line.split(",");
String name = lineArray[0];
int pure = Integer.parseInt(lineArray[2]);
if (name != null && (name.equals("refuse") || name.equals("being") ||
name.equals("poll") || name.equals("essential"))) {
bufferedWriter.write(name+","+pure);
bufferedWriter.newLine();
}
}
bufferedWriter.flush();
} else {
System.out.println("Input File not found");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (sacnnerReader != null) {
sacnnerReader.close();
}
if (bufferedWriter != null) {
bufferedWriter.close();
}
if (fileWriter != null) {
fileWriter.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

****** ProcessCSVFile Class ********


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class ProcessCSVFile {
public static void main(String[] args) {
q3();
}
public static void q3() {
Scanner sacnnerReader = null;
BufferedWriter bufferedWriter = null;
FileWriter fileWriter = null;
try {
File file = new File("properties.csv");
if (file.exists()) {
// creating File Writer to write CSV file
fileWriter = new FileWriter("output.csv");
// creating Buffered Writer to write CSV file
bufferedWriter = new BufferedWriter(fileWriter);
// creating sacnner Reader to read CSV file
sacnnerReader = new Scanner(file);
while (sacnnerReader.hasNextLine()) {
String line = sacnnerReader.nextLine();
String lineArray[] = line.split(",");
String name = lineArray[0];
int pure = Integer.parseInt(lineArray[2]);
if (name != null && (name.equals("refuse") || name.equals("being") ||
name.equals("poll") || name.equals("essential"))) {
bufferedWriter.write(name+","+pure);
bufferedWriter.newLine();
}
}
bufferedWriter.flush();
} else {
System.out.println("Input File not found");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (sacnnerReader != null) {
sacnnerReader.close();
}
if (bufferedWriter != null) {
bufferedWriter.close();
}
if (fileWriter != null) {
fileWriter.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

****** properties.csv ******

***** output.csv *******

Add a comment
Know the answer?
Add Answer to:
*q3: Write a public static method named q3 that takes no parameters and has return type...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a public static method named getMaxOf2Ints that takes in 2 int arguments and returns the...

    Write a public static method named getMaxOf2Ints that takes in 2 int arguments and returns the Maximum of the 2 values Write a public static method named getMinOf2Ints that takes in 2 int arguments and returns the Minimum of the 2 values Write apublic static method named getMaxOf3Ints that takes in 3 int arguments and returns the Maximum of the 3 values Write a public static method named getMedianOf3Ints that takes in 3 int arguments and returns the Median Value...

  • write a static method named greet that takes a String that represents a name and prints...

    write a static method named greet that takes a String that represents a name and prints a personalized greeting to StdDraw. So far i have in the main method... 4 public class TakeHomeAssignment 5 { 6 public static void main(String[] args) 7 { 8 StdDraw.setScale(0,500); 9 //drawEye(250,250,50); 10 String greet(Aaron); 11 // drawStickFigure(250,250); and for the method; //public static String greet( name) 31 //{ 32 33 34 //StdDraw.text(250,250,"Hello" +name); 35 36 // } Im getting errors saying its an illegal...

  • 4. Write a static method named ConcatStrings that is passed any number of parameters of   type...

    4. Write a static method named ConcatStrings that is passed any number of parameters of   type String, and returns a single string that is the concatenation of the strings passed.   (4 pts.)   5. Write a static method named ConcatObjects that is passed any number of parameters of   type Object, and returns a single string that is the concatenation of the result of the call to   toString() for each of the objects. (4 pts.)   6. Write a class named Triplet that...

  • Write a function named "csv_to_list" that takes a string as a parameter representing the name of...

    Write a function named "csv_to_list" that takes a string as a parameter representing the name of a CSV file with 5 columns in the format "<int>,<int>,<int>,<int>,<int>" and returns a new list containing all the values in the third column as integers in the same order they appear in the input file //JAVASCRIPT //

  • Submit Chapter6.java with a public static method named justifyText that accepts two parameters; a Scanner representing...

    Submit Chapter6.java with a public static method named justifyText that accepts two parameters; a Scanner representing a file as the first parameter, and an int width specifying the output text width. Your method writes the text file contents to the console in full justification form (I'm sure you've seen this in Microsoft Word full.docx ).  For example, if a Scanner is reading an input file containing the following text: Four score and seven years ago our fathers brought forth on this...

  • pythin: Write a function named "computed_column" that takes a string as a parameter representing the name...

    pythin: Write a function named "computed_column" that takes a string as a parameter representing the name of a CSV file with 5 columns in the format ",,,," and writes a file named "blank.csv" containing all the data from the input file but with a sixth column containing the sum of the values from the second and fourth columns

  • Question 1[JAVA] Add a method in the Main class named getLines() that takes a filename as...

    Question 1[JAVA] Add a method in the Main class named getLines() that takes a filename as a String and returns each line in the file in an array. Have each element of the array be a String. Do not include the newline at the end of each line. Use a BufferedReader object to read the file contents. Note, the contents of input.txt will be different (including the number of lines) for each test case. Example: getLines( "input.txt" ) returns (an...

  • (a) FileIO In the FileIO class add a public static method named writeCharacter. This method takes...

    (a) FileIO In the FileIO class add a public static method named writeCharacter. This method takes as input a Character to write, and a String which is the filename to write to. Within this writeCharacter method, use the FileWriter and BufferedWriter objects to write the character’s information back to a file. Make sure to catch the IOException when writing a file, and throw an IllegalArgumentException with an appropriate error message. If you prefer, you can also use the throws statement...

  • 5)Write a public static void method named happyNewYear that takes in an int and does the...

    5)Write a public static void method named happyNewYear that takes in an int and does the following: ● prints each number as it counts down to zero ● If the number is odd, print the current number followed by“OUU!”. ● If the number is even, print the current number followed by “AAA!” ● When it reaches Zero, print “HAPPY NEW YEAR!”

  • Write a static method named findMin that accepts an ArrayList of Person objects named list. Each...

    Write a static method named findMin that accepts an ArrayList of Person objects named list. Each Person object has private myAge and myName properties and corresponding public getAge and getName accessors. The method must return the name of the youngest person. public static String findMin(ArrayList<Person>list) {

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT