Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Putting an object instance into an ArrayList

 
Shamsudeen Akanbi
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 85
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 85
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay got it
 
Shamsudeen Akanbi
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic