Matt
Matt

Reputation: 57

Array Lists, reading in from file and 2 Classes

I'm learning arraylists, I'm unsure of how to read in from file and add it to a list as I am much more used to arrays, are they alike?

I'm also getting many errors when I am trying to instantiate the class object 'film' but never mind about it.

How am I able to get load my file method working? To me it looks right I think I just need a strangers pov.

Also getting an error when trying to find the file symbol. If there is any specific readings I should do for array lists could you please link me or explain best you can.

I'm very new to both coding and stack overflow so if you could dumb anything down and please be patient if I don't understand anything thanks.

import java.util.*;
public class CinemaDriver {

   film[] Film;

   public static void main(String[] args) {
      Film = new film[100];
      ArrayList <Film> list = new ArrayList<Film> ();
   }

   public void readFromFile() {
      File f = new file("file.txt");
      Scanner infile = new Scanner(f);
      int x = infile.nextInt();
      for(int i = 0; i < x ; i++) {
         String title = infile.nextLine();
         String genre = infile.nextLine();
         int screenings = infile.nextInt();
         int attendance = infile.nextInt();
         file.nextLine();
         list.add(title,genre,screenings,name);
      }
      infile.close();
   }

   public void displayAll() {
      for (film f : list ){
         System.out.println(f +"/ \n");
      }
   }
}

Upvotes: 2

Views: 118

Answers (1)

SCouto
SCouto

Reputation: 7928

Your ArrayList keeps Film objects as defined here:

ArrayList <Film> list = new ArrayList<Film> ();

But you are trying to insert several different objects or values (Strings, ints, etc...) instead of a Film object

list.add(title,genre,screenings,name);

What you should do is something like this:

Option 1:

list.add(new Film(title,genre,screenings,name));

Option2:

Film f = new Film();
f.setTitle(title);
f.setGenre(genre);
f.setScreenings(screenings);
f.setName(name);
list.add(f);

Upvotes: 3

Related Questions