File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Putting an object instance into an ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Putting an object instance into an ArrayList" Watch "Putting an object instance into an ArrayList" New topic
Author

Putting an object instance into an ArrayList

Shamsudeen Akanbi
Ranch Hand

Joined: Dec 24, 2010
Posts: 34
Hi guys, in this home automation application, I want to create an instance of DVDInfo for each line of data I read in from the dvdInfo.txt file. For each instance, I will parse the line of data and populate DVDInfo's three instance variables. Finally, I want to put all of the DVDInfo instances into an ArrayList.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 3133
Do you have a question?

What have you tried so far and what part(s) gave you trouble? And exactly what kind of trouble?
Shamsudeen Akanbi
Ranch Hand

Joined: Dec 24, 2010
Posts: 34
hey sorry I thought I added it! Here it is:


import java.util.*;
import java.io.*;
class DVDInfo {
private String title;
private String genre;
private String leadActor;

DVDInfo(String t,String g,String a) {
title = t;
genre = g;
leadActor = a;
}
public String toString() {
return title + " " + genre + " " + leadActor + "\n";
}
public String getTitle() {
return title;
}
public String getGenre() {
return genre;
}
public String getLeadActor() {
return leadActor;
}
public static void populateList() {
try {
File dvdDir = new File("dvdDir");
dvdDir.mkdir();
File dvdFile = new File("dvdDir", "dvdInfo.txt");
dvdFile.createNewFile();
FileWriter fw = new FileWriter(dvdFile);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("Donnie Darko/sci-fi/Gyllenhall, Jake");
bw.newLine();
bw.write("Raiders of the Lost Ark/action/Ford, Harrison");
bw.newLine();
bw.write("2001/sci-fi/??");
bw.newLine();
bw.write("Caddy Shack/comedy/Murray, Bill");
bw.newLine();
bw.write("Star Wars/sci-fi/Ford, Harrison");
bw.newLine();
bw.write("Lost in Translation/comedy/Murray, Bill");
bw.newLine();
bw.write("Patriot Games/action/Ford, Harrison");
bw.flush();
bw.close();

FileReader fr = new FileReader(dvdFile);
BufferedReader br = new BufferedReader(fr);
String data;
while( (data = br.readLine()) != null) { //prints each line of
//text from the file

String[] token = data.split("/"); //splits the string
DVDInfo dvd = new DVDInfo(token[0], token[1], token[2]);

System.out.println(dvd);
}
fr.close();
} catch(IOException e) { e.printStackTrace(); }
}
public static void main(String[] args) {
ArrayList<DVDInfo> dvdList = new ArrayList<DVDInfo>();
populateList();
System.out.println(dvdList);

}
}

Hey bro, its well edited now. I don't understand how to put each instances of the DVDInfo I read and splited into the ArrayList.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 3133
1) Please go back and edit your post to UseCodeTags.(⇐click) You'll probably need to re-paste it, since the indenting will already have been lost.

2) You still haven't answered these two important questions that I asked in my first reply: What part(s) gave you trouble? And exactly what kind of trouble?
Shamsudeen Akanbi
Ranch Hand

Joined: Dec 24, 2010
Posts: 34
okay got it
Shamsudeen Akanbi
Ranch Hand

Joined: Dec 24, 2010
Posts: 34


Hey bro, its well edited now. I don't understand how to put each instances of the DVDInfo I read and splited into the ArrayList.
Mohamed Sanaulla
Bartender

Joined: Sep 08, 2007
Posts: 2694

Hi Shamsudeen, I have edited the post and reformatted the code for you, so that its more clearer for the person reading it.

Coming back to your query: you are creating a new ArrayList instance but your populateList method is not aware of this instance because the scope of the reference variable you are using is limited to the main() method. You can pass around the reference to the populateList method and let the method take care of adding the DVDInfo to the list. So it would be something like:



PS: the code I have given above is syntactically not correct, its just an idea of how you can fix your code.


Mohamed Sanaulla | My Blog
 
 
subject: Putting an object instance into an ArrayList
 
Threads others viewed
Sorting Collections
Collections.binarySearch(...)
How to pass a constructor using arraylist
generics
How to use Collections.binarySearch()
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com