Reputation: 1
I'm in need of a little help with this project I'm trying to complete. Whenever I try output my array of student objects with the data fields of:
(Student ID, First name , Last name, Date of birth, Final mark, Grade).
I'm pretty sure it has got to do with my toString() method and the format of it, maybe I should create another method to use for the output format instead of using toString() which is the format I want to print to the screen.
My goal outcome with this question I'm posting now is to have each subject in each row and each piece of data such as Student ID, First name and so on to be in separate columns in the .csv file or in other words an excel spreadsheet.
Here is a snippet of my code and if someone could help me figure out a method or two to output the information within the array of objects correctly to a .csv file that would be greatly appreciated! Thank you in advance!
public class NewClass {
public static void main(String[] args) {
static PostGraduateStudent[] PGstudentArray;
}
private static void getPGStudentDetails() {
PostGraduateStudent s1 = new PostGraduateStudent();
s1.setName("Vasiliy", "Zaytsev");
s1.setDateOfBirth(02, 5, 1993);
s1.setID(25643924);
s1.setTitle("Mr");
s1.setEveryMark(50, 50, 50);
s1.calcMarks();
s1.calcGrade();
// System.out.println(s1);
PostGraduateStudent s2 = new PostGraduateStudent();
s2.setName("Joseph", "Stojk");
s2.setDateOfBirth(29, 12, 1990);
s2.setID(32454523);
s2.setTitle("Mr");
s2.setEveryMark(80, 70, 60);
s2.calcMarks();
s2.calcGrade();
// System.out.println(s2);
PostGraduateStudent s3 = new PostGraduateStudent();
s3.setName("Arkan", "Tigrovi");
s3.setDateOfBirth(23, 10, 1992);
s3.setID(83957293);
s3.setTitle("Mr");
s3.setEveryMark(60, 60, 70);
s3.calcMarks();
s3.calcGrade();
// System.out.println(s3);
PostGraduateStudent s4 = new PostGraduateStudent();
s4.setName("Draza", "Mihailovic");
s4.setDateOfBirth(23, 3, 1988);
s4.setID(13121389);
s4.setTitle("Mr");
s4.setEveryMark(99, 98, 99);
s4.calcMarks();
s4.calcGrade();
// System.out.println(s4);
PostGraduateStudent s5 = new PostGraduateStudent();
s5.setName("Dragana", "Mirkovic");
s5.setDateOfBirth(10, 9, 1979);
s5.setID(64926748);
s5.setTitle("Mr");
s5.setEveryMark(24, 26, 100);
s5.calcMarks();
s5.calcGrade();
//System.out.println(s5);
PostGraduateStudent s6 = new PostGraduateStudent();
s6.setName("Stefan", "Jokic");
s6.setDateOfBirth(10, 9, 1980);
s6.setID(4829464);
s6.setTitle("Mr");
s6.setEveryMark(98, 80, 98);
s6.calcMarks();
s6.calcGrade();
PGstudentArray = new PostGraduateStudent[] { s1, s2, s3, s4, s5, s6 };
}
private static void outputPGToFile() {
try {
FileOutputStream f = new FileOutputStream(new File("PostgradStudent.csv"));
ObjectOutputStream o = new ObjectOutputStream(f);
//Writes the objects to the file
for(int i = 0; i < PGstudentArray.length; i++) {
o.writeObject(PGstudentArray[i].toString());
}
o.close();
f.close();
} catch (FileNotFoundException e) {
System.out.println("File not found");
} catch (IOException e) {
System.out.println("Error initialising the stream");
}
}
}
//ToString function in my student class
@Override //Simple method to select format of output and output the instance variables
public String toString() {
String output = String.format("%5d %2d-%2d-%2d %-3s %-5s %-5s %3d %2s", this.studentID, this.day_dob, this.month_dob, this.year_dob, this.title, this.firstName, this.lastName, this.marks, this.grade);
return output;
}
This is an example of the output that I get for postgrad students. I couldn't post a image because I don't have 10 reps so here's the link to it :) https://gyazo.com/f3b1d23276607b0bb1edb890e5ddafef
Upvotes: 0
Views: 569
Reputation: 15163
While I suggested in my comment that you use a BufferedWriter
, I think it is easier to use Files.write
.
With that, your outputPGToFile()
would look like this:
private static void outputPGToFile() {
try {
Files.write(Paths.get("PostgradStudent.csv"),
Arrays.stream(PGstudentArray).map(Object::toString).collect(Collectors.toList()),
StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
}
}
(You need to add the appropriate imports)
If you really need to use the old, low-level java.io
things, you can use this:
private static void outputPGToFile() {
try (FileWriter fw = new FileWriter(new File("PostgradStudent.csv"));
BufferedWriter bw = new BufferedWriter(fw)) {
for (PostGraduateStudent student : PGstudentArray) {
bw.write(student.toString());
bw.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}
The try-with-resources will make sure that the both the FileWriter
and the BufferedWriter
are closed, even when an exception occurs.
Upvotes: 1