How do I import a list file?
I want to import a list file into my array of objects
myFavouriteMovies is an array of objects of the class Movie; the array itself is of the class MovieList
MOVIES LIST
===========
Alphonso Bow (2009) 2009
Alphorn, Das (2003) 2003
Alpine Antics (1929) 1929
Alpine Antics (1936) 1936
Alpine Cabaret (1937) 1937
Alpine Climbers (1936) 1936
Alpine Echo, An (1909) 1909
Alpine Flapper, An (1926) 1926
Alpine for You (1951) 1951
Alpine Lease, The (1911) 1911
Alpine Love Call (1929) 1929
Alpine Paradise, An (1925) 1925
Alpine Rendezvous (1936) 1936
Alpine Retreat, An (1910) 1910
I can import a csv file formated like
#Title (year),Votes,Rating
The Shawshank Redemption (1994),524838,9.2
The Godfather (1972),414578,9.1
The Godfather: Part II (1974),247827,9
Inception (2010),201440,9
Pulp Fiction (1994),421687,8.9
Schindler's List (1993),278909,8.9
12 Angry Men (1957),120758,8.8
using the code
Code:
private void importTop(MovieList movieDatabase){
String year;
String title;
String row;
String[] values = new String[4];
JFileChooser cf = new JFileChooser();
int result = cf.showOpenDialog(null);
File file = null;
if(result == JFileChooser.APPROVE_OPTION)
file = cf.getSelectedFile();
try {
BufferedReader in = new BufferedReader(new FileReader(file));
while ((row = in.readLine()) != null){
values = row.split(",");
title = values[0];
int year_beg = title.indexOf("(")+1;
int year_end = title.indexOf(")", year_beg);
year = title.substring(year_beg, year_end);
Movie importedMovies = new Movie(values[0], values[1], values[2], year);
movieDatabase.add(importedMovies);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I can split the fields and pass those as attributes into the constructor of Movie... I want to split the title and year in the list file too, but it's formatted differently and doesnt have comma's how would I do this?