java - ClassCastException when casting array to readObject -


i have run array of object student (where there's 3 students):

        try     {         out = new objectoutputstream (new bufferedoutputstream (new fileoutputstream ("students.dat")));         out.writeobject(s[0]);         out.writeobject(s[1]);         out.writeobject(s[2]);         out.close();     }     catch (ioexception e)     {         system.out.println("error writing student data file.");     } } 

and each student object must set this:

for (int = 0; i<3; i++)     {         system.out.println("the following information applies student " + (i+1));         system.out.println("what student's name?");         string name = input.nextline();         if (i == 0)         {             system.out.println("please enter name again.");         }         name = input.nextline();         system.out.println("what social security number of student?");         string ssn = input.next();         system.out.println("how many courses has student completed?");         int numcourses = input.nextint();         int [] grades = new int [numcourses];         int credits = (5*numcourses);          double points = 0;         for(int k = 0; k<numcourses; k++)         {             system.out.println("type number represent letter grade of course " + (k+1) + ". type 1 a. type 2 b. type 3 c. type 4 d. type 5 f.");             grades[k] = input.nextint();             switch (grades[k])             {                 case 1:                     points += 4;                     break;                 case 2:                     points += 3;                     break;                 case 3:                     points += 2;                     break;                 case 4:                     points += 1;                     break;                 case 5:                     break;             }              s[i] = new student (name, ssn, numcourses, grades, credits);         }     } 

i keep getting classcastexception when running these lines:

object obj = new object(); obj = in.readobject(); student[] ns = (student[])obj; 

the exception looks this:

java.lang.classcastexception: unitseven.student cannot cast [lunitseven.student; @ unitseven.studentgpa.main(studentgpa.java:21) 

and line 21 last line in code mentioned above. know how fix can cast properly? in advance help.

you're reading single student trying cast student[]. remove array reference:

student s = (student)obj; 

this because you're storing each element of array in objectoutputstream itself, noted here:

out = new objectoutputstream (new bufferedoutputstream (new fileoutputstream ("students.dat"))); //you write each reference of student out.writeobject(s[0]); out.writeobject(s[1]); out.writeobject(s[2]); out.close(); 

if want/need read array, store array well:

out = new objectoutputstream (new bufferedoutputstream (new fileoutputstream ("students.dat"))); out.writeobject(s); out.close(); 

so can read properly:

object obj = new object(); obj = in.readobject(); student[] ns = (student[])obj; 

or in single line:

student[] ns = (student[])in.readobject(); 

Comments

Popular posts from this blog

php - Admin SDK -- get information about the group -

dns - How To Use Custom Nameserver On Free Cloudflare? -

Python Error - TypeError: input expected at most 1 arguments, got 3 -