I have a Movie object where the data members are a String title, int year, and ArrayList actors. I am a bit confused on how I can add this ArrayList<String> to my tree. I am reading this information from a file for example:
Forrest Gump/1994/Tom Hanks
Star Wars/1977/Mark Hamill,Carrie Fisher,Harrison Ford
So far, I have been able to add everything else except for the ArrayList. I am thinking that I will need to also line.split the contents of the array. Also, some of the movies do not have multiple actors as shown in the example so I am not sure how to go about implementing this. I have tried a few different ways, but have ended up with an IndexOutOfBoundsException.
Here is what I have so far:
try{
Scanner read = new Scanner( new File("movies.txt") );
do{
ArrayList<String> actorList = new ArrayList<String>();
String line = read.nextLine();
String [] tokens = line.split("/");
//I think I need to add another split for commas here.
//actorList.add() here
tree.add( new Movie(tokens[0], Integer.parseInt(tokens[1]), actorList ));
}while( read.hasNext() );
read.close();
}catch( FileNotFoundException fnf ){
System.out.println("File not found.");
}
Her is my Constructor if needed:
public Movie( String t, int y, ArrayList<String> a ){
title = t;
year = y;
actors = a;
}
IndexOutOfBoundsException? Post your complete stacktrace. And yes, you need to convert yourtokens[2]toList.