Question

Implement a class CSVReader that reads a CSV file, and provide methods: int numbOfRows() int numberOfFields(int...

Implement a class CSVReader that reads a CSV file, and provide methods:

int numbOfRows()

int numberOfFields(int row)

String field(int row, int column)

Please use the CSVReader and CSVReaderTester class to complete the code. I have my own CSV files and cannot copy them to here. So if possible, just use a random CSV file.

CSVReader.java

import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;

/**
   Class to read and process the contents of a standard CSV file
*/
public class CSVReader
{
   ...
  
   /**
Constructor opens the input file and stores each line in
a list of Strings.
@param fileName the name of the csv file to be processed
   */
   public CSVReader(String fileName)
   {
...
   }
  
   /**
Returns the number of lines in the CSV file
@return the number of rows (lines) in the file
   */
   public int numberOfRows()
   {
...
   }
  
   /**
Returns the number of fields in a particular row
@param row the line number (0 <= row < number of lines)
@return the number of fields in the given row
   */
   public int numberOfFields(int row)
   {
...
   }
  
   /**
Returns the field in a particular row and colum
@param row the line number (0 <= row < number of lines)
@param column the column number (0 <= row < number of columns in row)
@return the number of fields in the given row
   */
   public String field(int row, int column)
   {
...
   }
}

CSVReaderTester.java

/**
   This program demonstrates and tests methods of the CSVReader class.
*/
public class CSVReaderTester
{
   public static void main(String[] args)
   {
CSVReader reader1 = new CSVReader("att2007.csv");
CSVReader reader2 = new CSVReader("quotes.csv");
  
System.out.println("Number of rows: " + reader1.numberOfRows());
System.out.println("Expected: 214");
System.out.println("Number of fields in row 10: " + reader1.numberOfFields(10));
System.out.println("Expected: 7");
System.out.println("Row 10, Col 2: " + reader1.field(10, 2));
System.out.println("Expected: 24.95");

System.out.println("Number of rows: " + reader2.numberOfRows());
System.out.println("Expected: 2");
System.out.println("Number of fields in row 1: " + reader2.numberOfFields(1));
System.out.println("Expected: 4");
System.out.println("Row 1, Col 2: " + reader2.field(1, 2));
System.out.println("Expected: Hello, World");
System.out.println("Row 1, Col 3: " + reader2.field(1, 3));
System.out.println("Expected: He asked: \"Quo vadis?\"");
System.out.println("Row 0, Col 3: " + reader2.field(0, 3));
System.out.println("Expected: \"..., that all men are created equal, ...\"");
   }
}

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

Just change the file path mentioned in the code before running it

import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;

/**
 Class to read and process the contents of a standard CSV file
 */


public class CSVReaderTester
{
    public static void main(String[] args)
    {
        CSVReader reader1 = new CSVReader("/Users/sample/Desktop/csv.csv");
        CSVReader reader2 = new CSVReader("/Users/sample/quotes.csv");

        System.out.println("Number of rows: " + reader1.numberOfRows());
        System.out.println("Expected: 214");
        System.out.println("Number of fields in row 10: " + reader1.numberOfFields(10));
        System.out.println("Expected: 7");
        System.out.println("Row 10, Col 2: " + reader1.field(10, 2));
        System.out.println("Expected: 24.95");

        System.out.println("Number of rows: " + reader2.numberOfRows());
        System.out.println("Expected: 2");
        System.out.println("Number of fields in row 1: " + reader2.numberOfFields(1));
        System.out.println("Expected: 4");
        System.out.println("Row 1, Col 2: " + reader2.field(1, 2));
        System.out.println("Expected: Hello, World");
        System.out.println("Row 1, Col 3: " + reader2.field(1, 3));
        System.out.println("Expected: He asked: \"Quo vadis?\"");
        System.out.println("Row 0, Col 3: " + reader2.field(0, 3));
        System.out.println("Expected: \"..., that all men are created equal, ...\"");
    }
}


class CSVReader
{
    static ArrayList<String > rows = new ArrayList<>();


    /**
     Constructor opens the input file and stores each line in
     a list of Strings.
     @param fileName the name of the csv file to be processed
     */
    public CSVReader(String fileName)
    {
        String line = "";

        int i=0;
        try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {

            while ((line = br.readLine()) != null) {
                rows.add(i, line);

                i++;

            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     Returns the number of lines in the CSV file
     @return the number of rows (lines) in the file
     */
    public int numberOfRows()
    {
        return rows.size();
    }

    /**
     Returns the number of fields in a particular row
     @param row the line number (0 <= row < number of lines)
     @return the number of fields in the given row
     */
    public int numberOfFields(int row)
    {

        String[] fields = rows.get(row-1).split(",");
        return fields.length;
    }

    /**
     Returns the field in a particular row and colum
     @param row the line number (0 <= row < number of lines)
     @param column the column number (0 <= row < number of columns in row)
     @return the number of fields in the given row
     */
    public String field(int row, int column)
    {
        String[] fields = rows.get(row-1).split(",");
        return fields[column-1];
    }
}


/**
 This program demonstrates and tests methods of the CSVReader class.
 */
Add a comment
Know the answer?
Add Answer to:
Implement a class CSVReader that reads a CSV file, and provide methods: int numbOfRows() int numberOfFields(int...
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
  • /** * A collection of methods related to multi-dimensional arrays. */ public class Array2Lab { /**...

    /** * A collection of methods related to multi-dimensional arrays. */ public class Array2Lab { /** * Return whether k is in list. * Precondition: the elements of list are not null. * @param list the array to be searched. * @param k the number to search for. * @return true if k is an element of list, and false otherwise. */ public static boolean contains(Object[][] list, Object k) { return false; }    /** * Create a String that...

  • In this assignment, we will be making a program that reads in customers' information, and create...

    In this assignment, we will be making a program that reads in customers' information, and create a movie theatre seating with a number of rows and columns specified by a user. Then it will attempt to assign each customer to a seat in a movie theatre. 1. First, you need to add one additional constructor method into Customer.java file. Method Description of the Method public Customer (String customerInfo) Constructs a Customer object using the string containing customer's info. Use the...

  • QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes...

    QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes to a file. Compile and run these programs. There are several ways to read/write text files. The examples shown here are just one way of achieving this. Look at the API for the BufferedReader class. The readline() method is a simple way to read the text file line by line. It returns null when the end of the file has been reached. https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html Look...

  • import java.util.Scanner; // TASK #1 Add the file I/O import statement here /** This class reads...

    import java.util.Scanner; // TASK #1 Add the file I/O import statement here /** This class reads numbers from a file, calculates the mean and standard deviation, and writes the results to a file. */ public class StatsDemo { // TASK #1 Add the throws clause public static void main(String[] args) { double sum = 0; // The sum of the numbers int count = 0; // The number of numbers added double mean = 0; // The average of the...

  • Ask the user for the name of a file and a word. Using the FileStats class,...

    Ask the user for the name of a file and a word. Using the FileStats class, show how many lines the file has and how many lines contain the text. Standard Input                 Files in the same directory romeo-and-juliet.txt the romeo-and-juliet.txt Required Output Enter a filename\n romeo-and-juliet.txt has 5268 lines\n Enter some text\n 1137 line(s) contain "the"\n Your Program's Output Enter a filename\n romeo-and-juliet.txt has 5268 lines\n Enter some text\n 553 line(s) contain "the"\n (Your output is too short.) My...

  • Step 1: Getting Started Create a new .java file named Lab12.java. At the beginning of this...

    Step 1: Getting Started Create a new .java file named Lab12.java. At the beginning of this file, include your assignment documentation code block. After the documentation block, you will need several import statements. import java.util.Scanner; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; Next, declare the Lab12 class and define the main function. public class Lab12 { public static void main (String [] args) { Step 2: Declaring Variables For this section of the lab, you will need to declare...

  • Java 1. Write a getCount method in the IntArrayWorker class that returns the count of the...

    Java 1. Write a getCount method in the IntArrayWorker class that returns the count of the number of times a passed integer value is found in the matrix. There is already a method to test this in IntArrayWorkerTester. Just uncomment the method testGetCount() and the call to it in the main method of IntArrayWorkerTester. 2. Write a getLargest method in the IntArrayWorker class that returns the largest value in the matrix. There is already a method to test this in...

  • Please use my code to implement the above instructions. My Grid class: import java.util.ArrayList; import java.util.Collections;...

    Please use my code to implement the above instructions. My Grid class: import java.util.ArrayList; import java.util.Collections; class Grid { private boolean bombGrid[][]; private int countGrid[][]; private int numRows; private int numColumns; private int numBombs; public Grid() { this(10, 10, 25); }    public Grid(int rows, int columns) { this(rows, columns, 25); }    public Grid(int rows, int columns, int numBombs) { this.numRows = rows; this.numColumns = columns; this.numBombs = numBombs; createBombGrid(); createCountGrid(); }    public int getNumRows() { return numRows;...

  • I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][]...

    I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][] t) A method that returns true if from left to right in any row, the integers are increasing, otherwise false. - columnValuesIncrease(int[][] t) A method that returns true if from top to bottom in any column, the integers are increasing, otherwise false. - isSetOf1toN(int[][] t) A method that returns true if the set of integers used is {1, 2, . . . , n}...

  • This is for Java. Create ONE method/function that will return an array containing the row and...

    This is for Java. Create ONE method/function that will return an array containing the row and column of the largest integer i the 2D array. If the largest number is located on row 2, column 1, the method needs to return row 2 and column one. Do not use more than one method. Use the code below as the main. Please comment any changes. in java Given the main, create a method that RETURNS the largest number found in the...

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