Note that there are some explanatory texts on larger screens.

plurals
  1. POReading from a text file to multiple arrays JAVA
    primarykey
    data
    text
    <p>Hi i'm having a little problem reading from a text file into arrays. In my program, it is saved with movies and actors, and when saved, a text file is created with movies first and then actors, with each movie and actor having a seperate line. </p> <p>My problem is occuring when trying to read up until a point so that all the movies go in a movies array and all the actors go into an actor array. I have tried putting a write(newline) under the last movie in the file and then doing a while loop until the line is null to then end that loop and go onto a new loop for actors until the end of the file but this does not work.</p> <p>Really am stuck trying to get this to work, i'm quite a beginner at java so any and all help would be appreciated.</p> <p>Thanks in advance.</p> <p>[EDIT]</p> <p>My problem is that everything in the file goes into one array, the movie array. Instead, I would like the movies to go into the movie array, and the actors to go into the actor array. Below is my code for saving and loading the file so far:</p> <pre><code>public void save(String fileName) throws FileNotFoundException { try { BufferedWriter writer = new BufferedWriter(new FileWriter(fileName)); for ( int i = 0; i &lt; nbrMovies; i++) { writer.write(movies[i].getName()); writer.newLine(); } writer.newLine(); for (int n = 0; n &lt; nbrActors; n++) { writer.write(actors[n].getFullName()); writer.newLine(); } writer.close(); } catch(IOException ex) { ex.printStackTrace(); } } public void load(String fileName) throws FileNotFoundException { try { BufferedReader reader = new BufferedReader(new FileReader(fileName)); String line = null; while ((line = reader.readLine()) != null) { addMovie(line); } reader.close(); } catch (Exception ex) { ex.printStackTrace(); } </code></pre> <p>}</p> <p>Thanks again :) [END EDIT]</p> <p>[EDIT2]</p> <p>Example file below :)</p> <pre><code>testmovie </code></pre> <p>testmovie2</p> <p>Firstname Secondname Firstname2 Secondname2</p> <p>[END EDIT2]</p>
    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.
    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