3

嗨,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 专家干杯。

4

3 回答 3

5

填充列表()

//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

            } 

此循环构造将缓冲区中的行分配给`Strin

于 2013-03-18T09:40:38.077 回答
2

在 / 上拆分线,您应该得到所需的值。试试 line.split("/");

于 2013-03-18T09:36:18.887 回答
2

您必须填充数据并将列表返回给您的主要功能:

 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;
       }
于 2013-03-18T09:52:35.450 回答