Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8689327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:37:08+00:00 2026-06-12T23:37:08+00:00

I have several objects which are assigned attributes based on user input. I then

  • 0

I have several objects which are assigned attributes based on user input. I then store those objects in a vector, and then write that vector to a file but on deserializing the stored vector, only the first object is read. Here’s the code that i have so far:

    public Vector<Cases> registerCase() {
    Vector<Cases> casesVector = new Vector<Cases>(10, 2);
    Scanner myCase = new Scanner(System.in);
    Scanner userChoice = new Scanner(System.in);
    System.out.println("HOW MANY CASES DO YOU WANT TO REGISTER?");
    int count = userChoice.nextInt();
    for (int i = 0; i < count; i++) {
        Cases newCase = new Cases();

        System.out.println("Enter the case name: ");
        newCase.caseName = myCase.nextLine();

        System.out.println("Enter the client's name: ");
        newCase.client = myCase.nextLine();

        System.out.println("Enter the case type: ");
        newCase.caseType = myCase.nextLine();

        if((newCase.caseType.equals("Major")) || newCase.caseType.equals("major")){
            newCase.closedCaseRevenue = majorCasePrice;
        }else {
            newCase.closedCaseRevenue = (int) (0.75 * majorCasePrice);
        }

        casesVector.add(newCase);
    }

    try{
        // Open a file to write to, named SavedCases.sav.
        FileOutputStream saveFile = new FileOutputStream("SavedCases.sav", true);
        ObjectOutputStream save = new ObjectOutputStream(saveFile);
        save.writeObject(casesVector);
        save.close(); 
    }
    catch(Exception exc){
        exc.printStackTrace(); 
    }

    Vector<Cases> comVector = new Vector<Cases>();
    try{
        FileInputStream saveFile = new FileInputStream("SavedCases.sav");
        ObjectInputStream save = new ObjectInputStream(saveFile);
        comVector = (Vector<Cases>) save.readObject();
        System.out.println("The size of the vector is: " + comVector.size());
        save.close(); // This also closes saveFile.
    }
    catch(Exception exc){
        exc.printStackTrace(); 
    }

    for (Cases law_case : comVector) {
        System.out.println("Name: " + law_case.caseName);
        System.out.println("Client: " + law_case.client);
        System.out.println("Case Type: " + law_case.caseType);
        System.out.println("Case State: " + law_case.caseState);
        System.out.println("Case Revenue: " + law_case.closedCaseRevenue);
        System.out.println();

    }

    return casesVector;

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:37:09+00:00Added an answer on June 12, 2026 at 11:37 pm

    EDIT: So to append to a vector if it already exists…

    Check for an existing file using

    File f = new File(FileName);
    if(f.exists()) 
      /* Read in the vector from the file using an object input stream on the file */
    else 
      /* make a new vector */
    

    Then read in your input and output it exactly as you have it there, however when you make the FileOutputStream do not include the true flag, this will cause you to add a new vector each time instead of just overwriting the current vector with the new, correct one.

    ORIGINAL

    The problem is with your implementation is that you are appending a new array each time you write to the save file. So whenever you try to read from the file, you are always getting that first array you ever made.

    I am not sure whether you’d like to just overwrite the array with a new one each time, but you should either read in the current array before you add more cases or not set the append flag to true for the FileWriter constructor depending on your desired result.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have users that have several objects and can upload images for those objects.
Here's the setup: I have several tables that hold information for data objects which
We have several domain objects which need to support both read-only and read-write modes;
So I have an array with objects which each have several attributes including an
I have several objects set up in Core Data, one of which is Deck
I have several TextField columns on my UserProfile object which contain JSON objects. I've
I have an object which has several properties that are set when the object
I have several objects in my app that can become nil at some point
I have a collection of objects which have several properties in each of them.
I have an array of FileReference objects which have several listeners attached to each

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.