Question: How would I write a java program that reads a csv file and add it to an already existing csv file. The format of the
How would I write a java program that reads a csv file and add it to an already existing csv file.
The format of the csv files are:
22 lines of data in the file, each line having exactly 4776 points of data. The first line says either "DP" or "CR" The third line is the patients ID. The rest of it is doubles.
import java.util.ArrayList;
public interface PatientCollectionADT {
// TODO Auto-generated method stub public Patient getPatient (String id) throws IOException;
// Return the patient with the given id. Return void if the id does
// not exist in the collection
public Patient removePatient (String id) throws IOException;
// Remove and return the Patient with the given id. Return void if the id does not exist.
public void setResultForPatient (String id, String result) throws IOException;
// Set the result field for the patient with given id.
public ArrayList getIds () throws IOException;
// Return an ArrayList with all of the collection's patient ids
public String addPatientsFromFile (String fileName) throws FileNotFoundException, IOException;
// Method reads all lines from comma separated file named fileName.
// Each line must contain a unique patient identifier followed by exactly 4776 doubles.
// If the line does not meet the criteria, it is not added to the patient collection,
// and an error message is included in the return String.
// The error message will indicate which line was in error and what the error was.
// expected line format
//id,protein1,protein2, ... , protein4776
public String toString ();
// Return a String representation of the collection.
// Only include the 3697th and 3258th values in that order.
}
The first file, called 'file1.csv', is the file we are adding to.
'file2.csv' contains a new batch of patients, which needs to be read, and added to file1.csv. The difference between file1 and 2 is that file2 doesn't have 'DP' or 'CR' in it, which is what this class is for:
public class Predictor {
public static String predict (double p1, double p2) {
if (p1 <= 20.903959) {
return "predDP";
}
else {
if (p2<= 22.058599) {
return "predCR";
}
else {
return "predDP";
}
}
}
p1 is the 3697th and p2 is the 3258th.
So it needs to iterate through a csv file through that many commas and take those entries per patients, output in the stringtostring the patient id, CR or DP, the 3697 and 3258th piece of data while also taking the data from another csv file and adding it to the existing one. The patient ID can stay the same.
The input for file1.csv looks like this:
CR,unknown,1,21.17421854,22.43033516,22.41361424,18.04736126,21.78633494,22.35560605,22.69549695,21.4620926,19.93962333... and so on 4,776 times.
Then it repeats with a new patient up to 21.
DP,unknown,21,20.27893815,22.86582667,22.67658889
The third column is the id, which is why it goes up each time.
The first column is CR or DP, the second column says unknown, but after running the code it should change to either CR or DP, depending on the predictor class. The third column is the patient id. The next 4,776 columns are doubles. Using the 3697th column and the 3258th column in the predictor class, the unknown column will change to DP or CR.
Sample output:
"EITHER CR OR DP",1,21.17421854,22.43033516,22.41361424,18.04736126,21.78633494,22.35560605,22.6954
"EITHER CR OR DP",21,20.27893815,22.86582667,22.67658889
We replace the first 2 columns with the result of the predictor class.
The input for file2.csv looks like this:
30,17.89243231,22.02656195,22.41252323,
It only has the id, so it will run use the 3695th and 3256th column
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
