2012-02-13 102 views
0

我想搜索鏈接列表的匹配,但我總是變得錯誤。這裏是我的代碼:搜索鏈接列表匹配

名單充滿INFILE是

try 
     { 
      BufferedReader infile = 
       new BufferedReader(new FileReader("C:\\videoDat.txt")); ..... 

     public static void createVideoList(BufferedReader infile, 
             VideoList videoList) 
             throws IOException 
    { 
     String title; 
     String star1; 
     String star2; 
     String producer; 
     String director; 
     String productionCo; 
     int InStock; 



     title = infile.readLine(); 
//If the title exists then there is the rest 
     while ((title=infile.readLine()) != null) { 
      // Fill Linked list 
      star1=infile.readLine(); 
      star2=infile.readLine(); 
      producer=infile.readLine(); 
      director=infile.readLine(); 
      productionCo=infile.readLine(); 
      InStock=infile.read(); 
      VideoElement MovieObject=new VideoElement(); 
      MovieObject.setVideoInfo(title,star1,star2,producer, 
      director,productionCo,InStock); 
      videoList.addToList(MovieObject); 
      title=infile.readLine(); 
     }//end while 
    }//end createVideoList 

搜索使用equals方法:

public boolean searchVideoList(String title) 
{ 

    for(VideoElement x:VideoList) 
    { 
     if(x.equals(title)) 
     { 
      return true; 
     } 

    } 
     return false; 


}//end searchVideoList 

從main方法調用搜索:

System.out.print("Enter the title: "); 
         title = in.readLine(); 
         System.out.println(); 
         if(videoList.searchVideoList(title)==true) 
          System.out.println("Title found."); 
         else 
          System.out.println("Video not in store."); 
         break; 

對不起我沒有連意識到它正在使用VideoElement的overided equals方法,它是由老師爲我們提供的:

public boolean equals(DataElement otherElement) 
    { 
     VideoElement temp = (VideoElement) otherElement; 
     return (videoTitle.compareTo(temp.videoTitle) == 0); 
    } 

我總是看不到視頻,因爲searchvideolist總是在最後返回false。但如果我刪除它告訴我,我需要返回的東西..怎麼辦..做什麼..

+0

你應該有在這裏兩個反斜槓: 嘗試{BufferedReader中的infile =新的BufferedReader(新的FileReader( 「C:\ videoDat.txt」)); 。 – JZares 2012-02-13 05:35:46

+0

我其實不知道爲什麼或者我怎麼沒有複製它們.. – Sackling 2012-02-13 05:38:44

+0

在上面的代碼中,您正在使用VideoElement.equals方法。所以問題是:(1)你是否重寫了equals方法? (2.)你爲什麼不在這裏使用比較器? – NiranjanBhat 2012-02-13 05:39:00

回答

1
  1. 是VideoElement擴展字符串以任何方式嗎?
  2. VideoElement中實現的equals()方法如何? - >你應該知道equals用於和相同類型的Object進行比較,所以除非VideoElement和String是相同的,否則你會得到錯誤。

試着改變你的方法是這樣的: 爲(VideoElement X:VideoList) { 如果(x.getTitle()等於(職稱)。) { ... }

0

您的文件C:\videoDat.txt是否包含由換行符('\ n'),回車符('\ r')或回車符後面緊跟的一個回車符(標題,星號1,星號2,生產者等)換行。 請確保使用BufferedReader.readLine(),因爲它讀取了一行文字。

而且在你的下面的代碼:

for(VideoElement x:VideoList) 
{ 
    if(x.equals(title)) 
    { 
     return true; 
    } 
    ..... 

您比較一個VideoElement對象xStringtitle。你嘗試糾正嗎?

+0

是每行文本在它後面都有一個回車符 – Sackling 2012-02-13 06:03:56

+0

在將標題添加到MovieObject之前,請嘗試調用title.trim()方法。這可能適用於比較。 – NiranjanBhat 2012-02-13 06:21:52

+0

我最終通過使用一個變量來修復它,然後返回該變量 – Sackling 2012-02-13 06:32:59

0

我弄成這樣固定的:

boolean videoSearch(String title){ 
    VideoElement temp = new VideoElement(); 
     boolean found = false; 
     for(ListIterator<VideoElement> Vitr = VideoList.listIterator();Vitr.hasNext();){ 
      temp=Vitr.next(); 
       if(temp.checkTitle(title)){ 
       //item is found 
        found = true; 
        break; 
       } 
       else 
        found=false; 
     }  

    return found; 

    }