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, ...\"");
}
}
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.
*/
Implement a class CSVReader that reads a CSV file, and provide methods: int numbOfRows() int numberOfFields(int...
I need help with the last method listed in the problem: Implement a class Grid that stores measurements in a rectangular grid. The grid has a given number of rows and columns, and a description string can be added for any grid location. Supply the following constructor and methods: public Grid(int numRows, int numColumns) public void add(int row, int column, String description) public String getDescription(int row, int column) public ArrayList getDescribedLocations() Here, Location is an inner class that encapsulates the...
/** * 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 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 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 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, 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 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 : Please help me correct my code: create a single class (Program11.java) with a main method and some auxiliary methods to input a 2-D array from a disk file, input some “transactions” to change the 2-D array and output the changed 2-D array to another file. Your main method will be minimal (see below). Most of the work will go on in your methods. In main, declare (but do not instantiate) 2-D array. Then call the three methods. The...
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. 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 IntArrayWorkerTester. Just uncomment...
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...