Consider class:
import java.util.*; import java.io.*; class CsvReader { //store the file name String filename; //store all the data in a file in the form of arraylist which contains arraylists of strings ArrayList> rows; //constructor CsvReader(String filename) throws Exception { try { //create a file object to read the file File csvfile = new File(filename); Scanner sc = new Scanner(csvfile); //create he object for array list to store the data in the file rows = new ArrayList>(); ArrayList columns; while(sc.hasNextLine()) { //for every row each string will be stored in columns arraylist and finally this arraylist will be added to //rows arraylist which contains the final data columns = new ArrayList(); //read the file line by line String line = sc.nextLine(); int flag = 0; int i = 0; String text = ""; while(i < line.length()) { //if there are not quotes and comma(,) has encountered means it is a string in the row if(flag == 0 && line.charAt(i) == ',') { columns.add(text); text = ""; } //if there is a quote then add the character to text else if(flag == 0 && line.charAt(i) == '\"') { flag = 1; text += line.charAt(i); } //if it is end of the quote add the character else if(flag == 1 && line.charAt(i) == '\"') { flag = 0; text += line.charAt(i); } else { text += line.charAt(i); } //increment the index value i++; } //add the string to columns arraylist columns.add(text); //add the columns arraylist to rows arraylist rows.add(columns); } } catch(Exception e) { System.out.println(e); } } //returns the number of lines in csv File int numberOfRows() { return this.rows.size(); } //returns the number of fields in a particular row int numberOfFields(int row) { return this.rows.get(row).size(); } //returns the field in a particular row or column String Field(int row, int column) { return this.rows.get(row).get(column); } } class CsvReaderTester { public static void main(String args[]) throws Exception { //tesing data //sample.csv contains the following data //1729, San Fransisco, "Hello, World", "He asked: ""Where are you going""" CsvReader csv1 = new CsvReader("sample.csv"); System.out.println(csv1.numberOfRows()); System.out.println(csv1.numberOfFields(0)); System.out.println(csv1.Field(0, 2)); //the files given in the question are not available to test the program } }
Consider the CSV data:
1, abc, defg
2, dfg, efg
3, hjk, eehj
question:
Using the CSVReader program provided above, to read the data and compute a summary, such as the maximum, minimum, or average of one of the columns.
output requirement:
max value -3
min value -1
average : 3
[code must be written in java language only.]