Results 1 to 2 of 2
Thread: How do I import a list file?
- 11-14-2010, 02:27 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 37
- Rep Power
- 0
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
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?Java 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(); } }
-
regular expressions could work well to help you do this. Are you familiar with them?
If not, please look here:
Regular Expressions Simple Tutorial
More Complex Regular Expressions Tutorial
Similar Threads
-
Import data from file into a gui
By SBL in forum AWT / SwingReplies: 5Last Post: 11-21-2009, 04:10 PM -
how to import xfdf file into the pdf file
By jkrishnanvenkat in forum New To JavaReplies: 3Last Post: 11-09-2009, 02:46 AM -
How to import file from same directory?
By Splat in forum New To JavaReplies: 5Last Post: 10-14-2009, 04:11 AM -
how to import a ejb bean in java file
By vijayinani in forum Enterprise JavaBeans (EJB)Replies: 2Last Post: 06-30-2009, 10:05 PM -
how to import war file with tomcat?
By makpandian in forum NetBeansReplies: 0Last Post: 12-31-2008, 11:00 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks