I want to read the text file and input data so that I can create the Patient class object from the input parameter. the Text file contains different lines & categories, the first few lines contain data for Patient class, the next few lines contain data for a drug class, the third category contains the physician data for physician class. as follows
"myFile.txt" is my file name and contain the following data
So I want to read this file and input the data so that I can create an object of the respective class for Patient object, drug object, and physician object. I tried in the following way but I cannot split the category. my program code read all files and input into the patient object. so anyone's help is appreciated.
Note: I have already created classes Patient, Drug & Physician in other class files, I have no problem with creating classes. What I only want is to read data from this Text file main class and build the class object. So my problem is only how to read the Text file line by line based on the class parameters and extract data from it as input for my class objects.
here is my code
import java.util.Scanner;
import java.util.ArrayList;
import java.io.File;
import java.io.FileNotFoundException;
public class Lsystem{
static ArrayList<Patient> pList;
static ArrayList<Drug> dList;
static ArrayList<Physician> plist;
public static void main(String[] args) throws FileNotFoundException{
pList = new ArrayList<Patient>();
dList = new ArrayList<Drug>();
plist = new ArrayList<Physician>() ;
File dataFile = new File("myFile.txt");
Scanner inputData = null;
try {
inputData = new Scanner(dataFile);
}
catch(Exception e) {
System.out.println(e);
System.exit(1);
}
// skip first line
String[] line = inputData.nextLine().split(" ");
//read next lines
while( inputData.hasNextLine()) {
if(!(inputData.hasNext("# drugs "))){
line = inputData.nextLine().split(",");
String name = line[0];
String securityNum = line[1];
Patient newPatient = new Patient(name,securityNum);
pList.add(newPatient);
}
}
for(Patient pas:pList){
System.out.println(pas);
}
else if ((inputData.hasNext("# drugs")))
while( inputData.hasNextLine() && (!(inputData.hasNext("# Physician ")))) {
line = inputData.nextLine().split(",");
String name = line[0];
String type = line[1];
double price = Double.parseDouble(line[2]);
double kg = Double.parseDouble(line[3]);
int conc = Integer.valueOf(line[4]);
Drug newDrug = new Drug(name,type,price,kg,conc);
dList.add(newDrug);
}
for(Drug drg:dList){
System.out.println(drg);
}
// an so on for Physician data as well
// .......
}
}