Using the "Open CSV" CSVReader library, read the provided csv file and calculate the average of its column 8 in the cons

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Using the "Open CSV" CSVReader library, read the provided csv file and calculate the average of its column 8 in the cons

Post by answerhappygod »

Using The Open Csv Csvreader Library Read The Provided Csv File And Calculate The Average Of Its Column 8 In The Cons 1
Using The Open Csv Csvreader Library Read The Provided Csv File And Calculate The Average Of Its Column 8 In The Cons 1 (144.18 KiB) Viewed 66 times
Using The Open Csv Csvreader Library Read The Provided Csv File And Calculate The Average Of Its Column 8 In The Cons 2
Using The Open Csv Csvreader Library Read The Provided Csv File And Calculate The Average Of Its Column 8 In The Cons 2 (111.94 KiB) Viewed 66 times
Using The Open Csv Csvreader Library Read The Provided Csv File And Calculate The Average Of Its Column 8 In The Cons 3
Using The Open Csv Csvreader Library Read The Provided Csv File And Calculate The Average Of Its Column 8 In The Cons 3 (11.24 KiB) Viewed 66 times
PLEASE HELP. JAVA PROGRAMMING
Using the "Open CSV" CSVReader library, read the provided csv file and calculate the average of its column 8 in the console. In this exercise you should use the "Open CSV" CSVReader library, which should be imported into IntelliJ using Maven (search for com.opencsv:opencsv:3.8). It's important to set up your main method with a try-catch block (please see the description of part2 of your Lab08). Inside the "try" part, set up a FileReader object. FileReader readMyFileObject = new FileReader ("Exercise08_Input.csv"); Then, create a CSVReader object that reads that file and is told that the data in the file is separated by the "comma" character as: CSVReader csvReader Object = new CSVReader (myFileObject, ','); Also, create a String array to capture one row at a time of the CSV file: String[] oneLineRecord; Then, create a while loop. Inside of the testing parentheses of the while loop write an expression that both 1. Transfers one line of the CSV object into your String array, and 2. Tests to make sure that the thing that was read isn't "null" (empty... the end of the file) (oneLineRecord = csvReader.readNext ()) != null Inside of your while loop you'll want to do the following: Extract an individual element (desired column) out of the String array as below and add it to a running sum variable. Double.parse Double (oneLineRecord []) · Feel free to print out that individual element, just so that you can see it. After the loop you can calculate average of the desire column. I Finally, you'll want to close both the CSVReader object and the file object: csvReader Object.close(); readMyFileObject.close(); Hint: The following flowchart and skeleton might be useful.
import java.io.filereader cam.opencsu. CSU Reader A class Method try Initialize Variables ↓ Create File Reader object ( Create CSVReader object Create a String array to capture one row of CSV Reader Object Loop Store next row of (start w 1st row) is row not equal to ? TRUE (data FALSE (no more data; exit loop) good) Copy one element in the row into a Catch Exception String array String Convert the String a Double add that Double to a ・running SUM increment by la counter average = into Running Sum Counter print average close CSV Reader Object close File Reader Object print exception found!
package Exercise08_Package; import java.io.FileReader; import com.opencsv. CSVReader; public class Exercise08_Main_Class { } public static void main(String[] args) { // try-catch is used in case reading the file fails. try { } // For reading files // Import with Maven: com.opencsv: opencsv: 3.8 } // Read the file. FileReader readMyFileObject = new FileReader("Exercise08_Input.csv"); CSVReader csvReaderObject = new CSVReader (readMyFileObject, ','); // Read one line at a time in the CSV while ( ) { } // Convert the extracted into a Double Float and use it elsewhere csvReaderObject.close(); readMyFileObject.close(); } catch (Exception ex) { System.out.println("Exception reading the file!");
If your program is written correctly, we should see the be message in the console. Total number of column 8 elements is 8783. The average of column 8 is 3.8815661266230084. Process finished with exit code 8
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply