2013-03-18 90 views
2

您好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專家。

回答

4

populateList()

//Signature of method changed to add List<DVDInfo> as a parameter  

    public static void populateList(List<DVDInfo> info) throws Exception { 
      BufferedReader br = new BufferedReader(new FileReader(new File("C:\\src\\dvdInfo.txt"))); 
      //read each line of dvdInfo.txt below 
      String line = null; 
      while ((line = br.readLine()) != null) { //check line in loop 
       String[] tokens = line.split("/"); 
       DVDInfo infoItem = new DVDInfo(tokens[0],tokens[1],tokens[2]); 
       info.add(infoItem);  
      } 
      } 

主()

//dvdList is now passed to the method. 
public static void main(String[] args) { 
    ArrayList<DVDInfo> dvdList= new ArrayList<DVDInfo>(); 
    try{ 
     populateList(dvdList); 
    }catch(Exception e){ 
     System.out.println("Exception caught"+e.getMessage()); 
    } 
    System.out.println(dvdList); 
} 

無限循環問題

代碼的這一部分將導致無限循環。

 String line=br.readLine(); 
     while(line !=null) { 
      DVDInfo = new DVDInfo(t,g,a); 

     } 

這是因爲從緩衝區讀取一行,然後在該行不爲空時執行循環。由於不會從緩衝區讀取新行,因此行永遠不會爲空,從而創建無限循環。這可以使用固定:

  String line = null; 
      while ((line = br.readLine()) != null) { //check line in loop 

      } 

這種循環結構分配從緩衝區`〜應變

+0

即時獲取以下錯誤 – Deepak 2013-03-18 09:54:19

+0

線程「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

+1

@Deepak看到更新,我很驚訝沒人發現 – 2013-03-18 10:00:46

2

將分隔線置於/上,您應該獲得所需的值。嘗試line.split(「/」);

+0

我無法在populateList()方法中做到這一點。你可以幫我使用填充列表方法中的代碼,你能完成populatelist方法嗎? – Deepak 2013-03-18 09:37:57

+0

String [] array = line.split(「/」); //這會給你數組中的元素,只是在構造函數中傳遞它們。 – 2013-03-18 09:39:17

+0

//第一步:爲我們從dvdInfo.txt文件中讀取的每一行數據創建一個DVDInfo實例//不能這樣做 //步驟2.對於每個DVDInfo實例,我們需要解析數據並填充DVDInfo的3個實例變量 - 無從得知。 //第3步。最後把所有的DVDInfo實例放入ArrayList ---我不知道如何繼續(不知道) – Deepak 2013-03-18 09:40:01

1

你必須填充數據並返回列表返回到您的主要功能:

public static ArrayList<DVDInfo> populateList() throws Exception { 
     ArrayList<DVDInfo> dvdList= new ArrayList<DVDInfo>();  
       BufferedReader br = new BufferedReader(new FileReader(new File("C:\\src\\dvdInfo.txt"))); 
       //read each line of dvdInfo.txt below 
       String line=br.readLine(); 


      while ((String line = br.readLine()) != null) { 
       //for each line use 
       String[] tokens = line.split("/"); 
       DVDInfo dvdInfo = new DVDInfo(tokens[0],tokens[1],tokens[2]); //step 1 & 2 
       dvdList.add(dvdInfo);//step 3 
      } 
      return dvdList; 
     } 
+0

線程「main」中的異常java.lang.OutOfMemoryError:java.util.ArrayList上的java.util.Arrays.copyOf(Unknown Source)上的java.util.Arrays.copyOf(Unknown Source)上的Java堆空間。在TestDVD.main處的TestDVD.populateList(TestDVD.java:74)處的java.util.ArrayList.add(未知源)處的java.util.ArrayList.ensureCapacityInternal(未知源)處的增長(未知源)(TestDVD.java:88 – Deepak 2013-03-18 09:59:29

+0

@Deepak只需重新啓動你的tomcat一次,然後再次運行程序。 – Lakshmi 2013-03-18 10:05:34

+0

這是一個單一的程序 – Deepak 2013-03-18 10:06:28