2012-12-26 56 views
-1

我正在使用Java S2SE製作一個簡單的原型項目。目標是製作一個文本文件,逐行閱讀,填充鏈接列表,然後要求用戶輸入名字和第二個名字。在Java中搜索LinkedList

文本文件的格式是:

firstnamesecondname手機之家移動電話2辦公

contact1contact2手機家園mobile2s辦公室

我在文本文件連接第一名稱及第二名稱。

然後我要求用戶輸入第一個和第二個名稱,並使用這兩個字符串的連接,將搜索已填充的鏈接列表。只要含有具有這些第一名字和第二名字的字符串的節點時,分割該節點,並顯示該節點的結果

我的代碼是:

try { 
    String fullname; 
    String mobile; 
    String home; 
    String mobile2; 
    String office; 

    Node current=first; 

    while(current.data == null ? key !=null : !current.data.equals(key)) { 
     String splitter[]=current.next.data.split(" "); 

     //fullname=splitter[9]; 
     mobile=splitter[1]; 
     home=splitter[2]; 
     mobile2=splitter[3]; 
     office=splitter[4]; 

     if(current.next.data.split(" ")==null?key==null:){ 

      mobilefield.setText(mobile); 
      homefield.setText(home); 
      mobilefield2.setText(mobile2); 
      officefield.setText(office); 
     } else { 
      throw new FileNotFoundException("SORRY RECORD NOT LISTED IN DATABASE"); 
     } 
      break; 
    } 
} catch(Exception e) { 
     JOptionPane.showMessageDialog(this,e.getMessage() 
     +"\nPLEASE TRY AGAIN !","Search Error", JOptionPane.ERROR_MESSAGE); 
} 

的問題是,所有會很好,但對於列表中的第一個和最多第n-1個節點,搜索錯誤,但是在該搜索中沒有到達最後一個節點。

+0

爲什麼不使用標準的java.util.List? –

+0

這個問題的解決方案實際上是在我面前散列圖片而不是鏈接列表。你實際上存儲在一個表中的密鑰,並將實際數據鏈接到密鑰...這就是哈希 – nitgeek

回答

0

匆匆一瞥,您試圖引用可能爲null的next節點。

current.next.data可能是null,如果是這樣的話,那麼你會得到一個NPE。

你的循環應該專注於它的工作對第i片 - 也就是大小ň的鏈表,當我得到的ñ一個節點,那麼我應該這個

此外,您不應該在沒有數據的節點中工作。跳過那些空的。

while(current.data != null) 

最後,提升您的current參考,否則你會無限循環。

current = current.next; 

另外一個觀察:

  • 在編輯時,我並沒有從上下文中知道,如果這兩條線應該是在同一行。如果是這種情況,那麼您會希望使用常量代替原始整數來指示哪裏發生了什麼 - 例如,您要查找的名稱將在[0]中,可以稱爲FULL_NAME
0

從我從你的問題中收集的內容中,我修改了你的代碼。這樣應該可以工作:

try { 
String fullname; 
String mobile; 
String home; 
String mobile2; 
String office; 

Node current; 

    //This loop will start from the first `Node`, check for `null`, then check if it is equal to the `key`. If not, then it will advance to `next` 
for(current=first; current!=null && !current.data.equals(key); current = current.next); 

    //the loop will terminate if current is null or is equal to key. If it is equal to key, we should display data. 
if(current.data.equals(key)) 
{ 

    String splitter[]= current.data.split(" "); 

    fullname=splitter[0]; 
    mobile=splitter[1]; 
    home=splitter[2]; 
    mobile2=splitter[3]; 
    office=splitter[4]; 

    mobilefield.setText(mobile); 
    homefield.setText(home); 
    mobilefield2.setText(mobile2); 
    officefield.setText(office); 
} 
else 
    throw new FileNotFoundException("SORRY RECORD NOT LISTED IN DATABASE"); 

} catch(Exception e) { 
    JOptionPane.showMessageDialog(this,e.getMessage() 
    +"\nPLEASE TRY AGAIN !","Search Error", JOptionPane.ERROR_MESSAGE); 

編輯:添加的註釋。

+0

Divyanshu感謝您的幫助,但現在它給nullpointer例外可以üPLZ幫助我在[email protected] pleaseeeeee其實我有一個項目,所以我需要幫助 – user1930526

+0

它只搜索列表中的最後一個節點:( – user1930526

+0

@ user1930526:我們所能做的只是提供提示,我們不是來幫你做作業的,你可以隨時要求澄清特定的答案,但不要給你的個人電子郵件地址尋求幫助 – Makoto