嗨,SO专家,
第 1 步。我创建了一个类 DVDInfo,它具有标题、流派、leadActor 以及相应的 getter 和 setter,如下面的代码所示。
问题:我必须从具有“正斜杠 /”的 dvdInfo.txt 文件中填充 Arraylist,并且所需的输出应该包含不带“正斜杠 /”的 arrayList。
------------------------------------
dvdInfo.txt file
------------------------------------
Donnie Darko/sci-fi/Gyllenhall, Jake
Raiders of the Lost Ark/action/Ford, Harrison
2001/sc-fi/??
Caddy Shack/comedy/Murray, Bill
Star Wars/sc-fi/Ford, Harrison
Lost in Translation/comedy/Murray, Bill
Patriot Games/action/Ford, Harrison
-------------------------------------
import java.util.*;
import java.io.*;
class DVDInfo{
private String title;
private String genre;
private String leadActor;
DVDInfo(String t,String g,String a){
this.title = t;
this.genre =g;
this.leadActor=a;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
public String getLeadActor() {
return leadActor;
}
public void setLeadActor(String leadActor) {
this.leadActor = leadActor;
}
@Override
public String toString() {
return "DVDInfo [title=" + title + ", genre=" + genre + ", leadActor="
+ leadActor + "]";
}
}
public class TestDVD {
/*
* //access dvdInfo.txt file and populate the arrayList by removing forward slash.
*/
public static void populateList() throws Exception {
BufferedReader br = new BufferedReader(new FileReader(new File("C:\\src\\dvdInfo.txt")));
//read each line of dvdInfo.txt below
String line=br.readLine();
while(line !=null) {
DVDInfo = new DVDInfo(t,g,a);
// Step 1.Wanted to create a DVDInfo Instance for each line of data we read in from dvdInfo.txt file.//failed to do so
// Step 2.for each DVDInfo Instance ,we need to parse the line of data and populate DVDInfo's 3 instance variables --no clue.
// Step 3.Finally put all DVDInfo instances into the ArrayList--- i dont know how to proceed(no clue)
}
}
public static void main(String[] args) {
ArrayList<DVDInfo> dvdList= new ArrayList<DVDInfo>();
try{
populateList();
}catch(Exception e){
System.out.println("Exception caught"+e.getMessage());
}
System.out.println(dvdList);
}
}
------------------------------------------------------
output of populateList should be without forward slash
------------------------------------------------------
Donnie Darko sci-fi Gyllenhall, Jake
Raiders of the Lost Ark action Ford, Harrison
2001 sc-fi ??
Caddy Shack comedy Murray, Bill
Star Wars sc-fi Ford, Harrison
Lost in Translation comedy Murray, Bill
Patriot Games action Ford, Harrison
请帮助我使用 java 1.5 版中 PopulateList 方法的代码,我对此感到困惑。
谢谢一切工作!向所有 SO 专家干杯。