Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Load ArrayList from file

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Load ArrayList from file

    Hello.

    I am trying to make a program which allows the user to store, manage, add, delete, etc CD Records from a Registry.
    The program includes a "CD" class which contains artist name, album name, etc. An arraylist keeps all these CD records saved.

    I am trying to get the program to automatically try to load a previously saved ArrayList from a file (if it exists), and then automatically save the now modified ArrayList back to the same file, once the user exits the program.

    I am now doing the loading from file method, but I cannot get it to work.

    	public void readFromFile(String fileName)
    	{
    		ArrayList<CD> reg = new ArrayList<CD>(); //Creates a new arraylist with CD records
     
    		try
    		{
    			Scanner scan = new Scanner(new File("filename"));
    			while (scan.hasNextLine()) //while file has more data..
    			{
    				reg.add(scan.next()); //read the next line
    			}
    		} 
    		catch (FileNotFoundException e)
    		{
    			System.err.println("File could not be opened!");
    			System.exit(1);
    		}
    		scan.close();
    	}

    The error I get is "The method add(CD) in the type ArrayList<CD> is not applicable for the arguments (String)".
    I understand that the scan is trying to read a textfile and cannot save that line (String) as a CD object. But I have no idea how to solve this.

    What should I do? The excersise advices me to use a Scanner for this part, so i'd prefer to follow that guideline. However, other suggestions (such as filestreamer) are also more than welcome as long as they help me solve this problem.

    Many thanks in advance.

    Regards,
    dcdude.


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Load ArrayList from file

    Hello dcdude!
    Can you give us more information of what does the textfile look like? If one row corresponds to one CD item and the CD's attributes (artist name, etc.) are somehow separated (ie with comma or a tab) then you can use the String's split method. Therefore you can take every separated member of the line, create a new CD of them and then store this CD in your ArrayList.
    Hope it helps.

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Load ArrayList from file

    Hi,
    thanks for quick reply.

    I haven't created the textfile yet. The layout of the textfile doesn't matter at all, as long as the ArrayList saves the data neccessary. I have attached the CD class below, I hope it helps you understand what needs to be stored:

    public class CD
    {
    	private String artist; // artist
    	private String title; // title
     
    	/** Creates a CD with the artist "artist and the title "title" */
    	public CD(String artist, String title)
    	{
    		this.artist = artist;
    		this.title = title;
    	}
     
    	/** Returns the name of the artist */
    	public String getArtist()
    	{
    		return artist;
    	}
     
    	/** Returns the title */
    	public String getTitle()
    	{
    		return title;
    	}
     
    	/** Returns a string which contains the CDs artist and title */
    	public String toString()
    	{
    		return artist + "\t" + title;
    	}
    }

    I will play around with your tip, and see what I can do. I have never used the string split method before...

    Thanks again!

  4. #4
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Load ArrayList from file

    You can store in each line of the textfile the CD's artist and title separated with whatever separator you want and then use that separator in your split method. Just read the javadoc and you will find out how the method works.

  5. #5
    Junior Member
    Join Date
    May 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Load ArrayList from file

    I understand how to separate the artist from the title.

    My problem is how to load the data from the text file (String) and put it into an ArrayList (CD). These are 2 different Objects.

    For the sake of example, I saved a txt file with the following content:
    ABBA:Waterloo

    The separator would be in this case the : sign. But "ABBA" and "Waterloo" would be loaded into the program as Strings, not as CD objects. That's my problem.

    Because of this, the following code wouldn't work:

    while (scan.hasNextLine()) //while file has more data..
    {
          reg.add(scan.next()); //read the next line
    }

    Thanks.
    Last edited by dcdude; May 13th, 2012 at 06:11 AM.

  6. #6
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Load ArrayList from file

    Did you try the split method? As I told you in post#2, it will give you the attributes (Strings) from which you will be able to create a new CD object and store it in your ArrayList.
    If you play arround with the split method you 'll get a better undestanding of what I said.

Similar Threads

  1. How to up load java file when you post??
    By zhen1606 in forum Collections and Generics
    Replies: 1
    Last Post: February 23rd, 2012, 09:12 AM
  2. How to load class path for .properties file
    By kewlkeny in forum Web Frameworks
    Replies: 1
    Last Post: January 23rd, 2012, 08:40 AM
  3. Simple game that requires me to load game settings from a file
    By 14fenix in forum Java Theory & Questions
    Replies: 5
    Last Post: December 1st, 2011, 09:21 PM
  4. quite small (make the changelog non-static, i.e. load its content from a file)
    By Abdallah in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: November 30th, 2009, 09:00 PM
  5. Arraylist Save and Load
    By frankycool in forum Collections and Generics
    Replies: 1
    Last Post: November 14th, 2009, 06:48 AM