您好SO專家除去forwardSlash,填充ArrayList和在期望的輸出
步驟1 .I已經創建了一個類DVDInfo具有標題,流派,leadActor與相應getter和setter如圖中下面的代碼。
問題:我必須從dvdInfo.txt文件中填入「正斜槓/」,並且所需的輸出應該包含不帶「正斜槓/」的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
請幫我PopulateList方法的Java版本1.5的代碼,我堅持這一點。
謝謝它所有的工作!歡呼所有SO專家。
即時獲取以下錯誤 – Deepak 2013-03-18 09:54:19
線程「main 「java.lang.OutOfMemoryError:Java堆空間 \t在java.util.Arrays.copyOf(來源不明) \t在java.util.Arrays.copyOf(來源不明) \t在java.util.ArrayList.grow(未知源) \t at java.util.ArrayList.ensureCapacityInternal(Unknown Source) \t at java.util.ArrayList.add(Unknown Source) \t at TestDVD.populateLi st(TestDVD.java:74) \t at TestDVD.main(TestDVD.java:88) – Deepak 2013-03-18 09:54:35
@Deepak看到更新,我很驚訝沒人發現 – 2013-03-18 10:00:46