Note that there are some explanatory texts on larger screens.

plurals
  1. POJava reading a vector of objects from a file only reads the first object in the vector
    primarykey
    data
    text
    <p>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:</p> <pre><code> public Vector&lt;Cases&gt; registerCase() { Vector&lt;Cases&gt; casesVector = new Vector&lt;Cases&gt;(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 &lt; 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&lt;Cases&gt; comVector = new Vector&lt;Cases&gt;(); try{ FileInputStream saveFile = new FileInputStream("SavedCases.sav"); ObjectInputStream save = new ObjectInputStream(saveFile); comVector = (Vector&lt;Cases&gt;) 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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload