Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Putting an object instance into an ArrayList

 
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay got it
 
Shamsudeen Akanbi
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Hot dog! An advertiser loves us THIS much:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic