Results 1 to 2 of 2
  1. #1
    Codeless is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default 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

    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();
    	        }
    		
    	}
    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?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Import data from file into a gui
    By SBL in forum AWT / Swing
    Replies: 5
    Last Post: 11-21-2009, 04:10 PM
  2. how to import xfdf file into the pdf file
    By jkrishnanvenkat in forum New To Java
    Replies: 3
    Last Post: 11-09-2009, 02:46 AM
  3. How to import file from same directory?
    By Splat in forum New To Java
    Replies: 5
    Last Post: 10-14-2009, 04:11 AM
  4. how to import a ejb bean in java file
    By vijayinani in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 06-30-2009, 10:05 PM
  5. how to import war file with tomcat?
    By makpandian in forum NetBeans
    Replies: 0
    Last Post: 12-31-2008, 11:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •