I want to use a textfile and take each line and put it into an classobject array. This is my code
try {
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("Patient.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
// Read file line by line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
}
// Close the input stream
in.close();
} catch (Exception e) { // Catch exception if any
System.err.println("Error: " + e.getMessage());
}
I need to transform this into an array object and this is how i want it to look like
Patient1 p[] = new Patient1[5];
p[0] = 001, "John", 17, 100, 65, 110, 110, 110, 109, 111, 114, 113, "Swaying, Nausea";
p[1] = 002, "Sun Min", 18, 101, 70, 113, 113, 110, 119, 111, 114, 113, "None";
and so on.
Building off of what AVD had suggested, you can accomplish what you want with a constructor that took in your values - although it's not suggested to use too many parameters in a constructor (for readability and debugging's sake). Depending on the way your data is ordered and read, you could even use String.split to get everything into one type (i.e. String).
public class Patient {
public Patient(String name, String id, String symptoms, String measurements) { to get the individual fields from using a delimiter.
// Stuff to fill in the fields goes here
}
}
You would invoke this by using the call new Patient("John, "001", "Swaying, Nausea", ...)
. Again, this depends on how you read the data coming in; if you cannot bunch the data up in a reasonable way, then you could also opt to create accessors and mutators.
You have to create Patient class with 13 fields, constructors and setter/getter.
public class Patient
{
private String field1;
private String field2;
private int field3;
....
public void setField1(String field1) { this.field1=field1; }
public String getField1() { return field1;}
...
}
and use ArrayList<Patient>
instead of array.
ArrayList<Patient> patients=new ArrayList<Patient>();
Patient pat=new Patient();
//set value to the patient object
patients.add(pat);