0

I am working a school project that basically allows the user to create a student, write that info in a binary file the the display method would read and display the info from the file. However, the first time a student is created is all good but the second student it gives me the following error:

Exception in thread "main" java.io.StreamCorruptedException: invalid type code: AC at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1743) at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:519) at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:477) at MidTermProject.DisplayStudent(MidTermProject.java:248) at MidTermProject.main(MidTermProject.java:353)

I read a little bit and since like it's trying to create a header but only 1 header can be written and that is why it throws an error, from reading it is clear what is creating it but nothing really clear about how to fix it, I read in the java book about private transient SomeClass RefVar; to be able to append data into the file, the idea is that the user can create as many students as they want, they would be written in the file and be able to display all of them, right now it only displays the first student, any ideas how to fix it? it would be appreciated, here is my code:

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.io.*;
import java.lang.ClassNotFoundException;
public class MidTermProject implements Serializable {
    
    static AtomicInteger idGenerator = new AtomicInteger(0001);
    static int id;
    
    public static int getId() {
        return id;
    }
    ///CreateStudent method
    public static void CreateStudent() throws IOException {
        String FullName;
        String address;
        String city;
        String state;
        String newStudentID;
        
        Scanner keyboard = new Scanner(System.in);
        
        FileOutputStream fstream =
                new FileOutputStream("StudentInfo.dat", true);
        ObjectOutputStream outputFile =
                new ObjectOutputStream(fstream);
        
        id = idGenerator.getAndIncrement();
        ///Convert int into a String and write it to the file
        String student = Integer.toString(getId());
        outputFile.writeObject("Student ID: " + student);
        
        System.out.print("\nPlease enter your information bellow.\n" + "\nFull Name: ");
        FullName = keyboard.nextLine();
        outputFile.writeObject("Full Name: " + FullName);
        
        System.out.print("Address: ");
        address = keyboard.nextLine();
        outputFile.writeObject("Address: " + address);
        
        System.out.print("City: ");
        city = keyboard.nextLine();
        outputFile.writeObject("City: " + city);
        
        System.out.print("State: ");
        state = keyboard.nextLine();
        outputFile.writeObject("State: " + state);
    
        outputFile.close();
        
        System.out.print("\nDone\n");
        
    }
    ///DisplayStudent method
    public static void DisplayStudent() throws IOException {
        FileInputStream fstream = new FileInputStream("StudentInfo.dat");
        ObjectInputStream inputFile = new ObjectInputStream(fstream);
        
        String student;
        boolean endOfFile = false;
        
        while(!endOfFile)
        {
            try
            {
                student = (String) inputFile.readObject();
                System.out.print(student + "\n");
            }
            catch (EOFException | ClassNotFoundException e)
            {
                endOfFile = true;
            }
        }
        System.out.println("\nDone");
        
        inputFile.close();
    }
    public static void main(String[] args) throws IOException {
        
        int start = 0;
        
        while(start >= 0) {
            Scanner keyboard = new Scanner(System.in);
            
            System.out.println("Here is the sample of menu choices for Main Menu.");
            
            System.out.println("\nWelcome to University Enrollment" + "\n1. Create Student" +
                                "\n2. Create Course" + "\n3. Create Enrollment" + "\n4. Edit Student" + "\n5. Edit Course"
                                + "\n6. Edit Enrollment" + "\n7. Display Student" + "\n8. Display Course" + "\n9. Display Enrollment"
                                + "\n10. Grades Sub Menu" + "\n0. --- Quit ---");
            
            System.out.println("Please enter a valid choice(1-10, 0 to Quit) :");
            int userInput = keyboard.nextInt();
            
                if(userInput == 1) {
                    CreateStudent();
                } else if(userInput == 2) {
                    CreateCourse();
                } else if(userInput == 3) {
                    CreateEnrollment();
                } else if(userInput == 4 ) {
                    EditStudent();
                } else if(userInput == 5) {
                    EditCourse();
                } else if(userInput == 6) {
                    EditEnrollment();
                } else if(userInput == 7) {
                    DisplayStudent();
                } else if(userInput == 8) {
                    DisplayCourse();
                } else if(userInput == 9) {
                    DisplayEnrollment();
                } else if(userInput == 10) {
                    GradesSubMenu();
                } else if(userInput == 0) {
                    System.out.print("Done\n");
                } else {
                    while(userInput > 10) {
                        System.out.println("Invalid Option, Please try again.");
                        userInput = keyboard.nextInt();
                        if(userInput == 1) {
                            CreateStudent();
                        } else if(userInput == 2) {
                            CreateCourse();
                        } else if(userInput == 3) {
                            CreateEnrollment();
                        } else if(userInput == 4 ) {
                            EditStudent();
                        } else if(userInput == 5) {
                            EditCourse();
                        } else if(userInput == 6) {
                            EditEnrollment();
                        } else if(userInput == 7) {
                            DisplayStudent();
                        } else if(userInput == 8) {
                            DisplayCourse();
                        } else if(userInput == 9) {
                            DisplayEnrollment();
                        } else if(userInput == 10) {
                            GradesSubMenu();
                        } else if(userInput == 0) {
                            System.out.print("Done\n");
                        }
                    }
                }
           }
    }
Tom Ronaldo
  • 21
  • 1
  • 4

0 Answers0