My two classes are designed too create an array of StudentData objects (name, Date Of Birth, and ID), that include a toString override to print out all variables. It then serializes the array and saves it to a file named studentdata.txt. It should then read the array from the file and reconstruct a new array object from this data, and print out each item in the array to the console.
Am getting this error when attempting to complie...
Exception in thread "main" java.io.NotSerializableException: Student
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeArray(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at StudentData.main(StudentData.java:38)
Also am not sure how to properly loop through my array and call my toString method to print out to the console, would i be right in assuming I should use a for each loop? Like this?
//for (Student s : ???) {
//System.out.println(How do I call toString from here?);
My classes
import java.io.*; //importing input-output files
class Student
{
String name; //declaration of variables
String DOB;
int id;
Student(String naam,int idno, String dob) //Initialising variables to user data
{
name=naam;
id=idno;
DOB=dob;
}
public String toString() {
return name+"\t"+id+"\t"+DOB+"\t";
}
}
Number 2
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
class StudentData //main class
{
public static void main(String args[]) throws IOException //exception handling
{
System.out.println("Enter the numbers of students:");
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(in.readLine());
Student[] S=new Student[n]; // array of objects declared and defined
for (int i = 0; i < S.length; i++) {
System.out.println("Enter the Details of Student no: "+(i+1)); //reading data form the user
System.out.println("Name: ");
String naam=in.readLine();
System.out.println("ID no: ");
int idno=Integer.parseInt(in.readLine());
System.out.println("DOB: ");
String dob=(in.readLine());
S[i]=new Student(naam,idno,dob);
File studentFile = new File("StudentData.txt");
try {
FileOutputStream fileOutput = new FileOutputStream(studentFile);
ObjectOutputStream objectOutput = new ObjectOutputStream(fileOutput);
objectOutput.writeObject(S);
S = null;
FileInputStream fileInput = new FileInputStream(studentFile);
ObjectInputStream objectInputStream = new ObjectInputStream(fileInput);
S = (Student[]) objectInputStream.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//for (Student s : ???) {
//System.out.println();
}
}
}
To get rid of the NotSerializableException: Student
exception, just make your Student
class implement the Serializable
interface. (Note that this is a marker interface, so you don't have to implement any methods.)
class Student implements Serializable {
}
To loop through the array S
of Student
objects, try this:
for (Student st : S) {
System.out.println(st);
}
I'd pick a more descriptive name for the array though (students
would be better). If you declare the array as
Student[] students = new Student[n];
then your for loop can be more readable.
for (Student student : students) {
System.out.println(student);
}
Note that you don't have to explicitly call the toString
method on an object of type Student
. Java will do that implicitly for you when you use an object as the parameter to a method that expects a String
.