2012-06-14 59 views
0

我需要搜索一個名稱的對象數組,然後輸出與該名稱相對應的所有信息。搜索每個記錄具有多個值的類數組

我已經

public class AccessFriendlyFile { 
private Friendlies[] fr = new Friendlies[100]; 
private int size = 0; 

public AccessFriendlyFile(){ 
try { 
    Scanner scFile = new Scanner(new File("Friends.txt")); 
    String line, name, surname, cell, mail, landline; 

    while (scFile.hasNext()){ 
     line = scFile.nextLine(); 
     Scanner sc = new Scanner(line).useDelimiter("#"); 
     name = sc.next(); 
     surname = sc.next(); 
     cell = sc.next(); 

     if (sc.hasNext()){ 
      mail = sc.next(); 
      landline= sc.next(); 
      fr[size] = new ExtendFriendlies(name, surname, cell, mail, landline); 

     } 

     else { 
      fr[size]= new Friendlies(name, surname, cell); 
     } 

     size++; 
     sc.close(); 
    } 

}catch (FileNotFoundException ex){ 
    System.out.println("File not found"); 
} 

我如何代碼,將搜索「FR」的名稱,並打印出所有相應的信息的方法?

非常感謝 傑西

編輯: 這裏是我的搜索方法,就是目前沒有工作。

public int Search(String name) { 
    int loop = 0; 
    int pos = -1; 
    boolean found = false; 
    while (found == false) { 
     if (fr[loop] == name) { 
      found = true; 
      pos = loop; 
     } else { 
      loop++; 
     } 
    } 

    return pos; 
} 

if語句中的不可比較類型錯誤。

+2

帶循環。你的代碼中已經有了一個循環,所以你應該知道它們是如何工作的。它是功課嗎?如此,請添加作業標籤。任何你不使用集合而不是數組的原因? –

+0

不是作業,試圖瞭解如何去做,完全是。我會到達那裏,慢慢向前移動。我將編輯以顯示我已經能夠提出的搜索方法。 – rakeshisu

回答

0

我建議你在這裏重命名你的變量。 Friendlies類商店,我認爲,一個聯繫人,一個朋友。 Friend對象列表是一個數組,您可能會名爲friendList或甚至friendlies。我也鼓勵你不要把大小作爲一個計數器變量。大小是你有多少朋友都有,你可以使用我,或friendCounter遍歷它們,或使用每個循環爲我演示下面,

public Friendlies find(String name) { 
    for(Friendlies friend : fr) { 
     if(friend.getName().equalsIgnoreCase(name)) 
      return fiend; 
    } 
    return null; 

} 

//now to print the info you can do this: 
Friendlies findJoe = find("Joe"); 
if(findJoe==null) 
    System.out.println("You have no friends namd Joe."); 
else 
    System.out.println(findJoe); 

我的代碼假定您實現的toString()的友誼賽。如果你使用netbeans,你可以自動生成這個代碼,然後調整它來獲得你想要的格式。 (只需右鍵單擊要編寫該方法的位置並選擇插入代碼)

+0

google guava的Objects.toStringHelper對於實現toString方法非常簡單。 – jocelyn

+0

我已經實現了一個toString()。感謝您的提示。這是有道理的,我達到了我想要實現的目標,但是出於利益的考慮,我將如何返回朋友以及他在陣列中的位置? – rakeshisu

+0

你可以在包含朋友和代表位置的int的類中封裝惡魔。你也可以返回一個看起來像Pair 的元組對象 – jocelyn

1

在你Friendlies類中,有一個名爲getName()方法,該方法將返回友好的名稱。遍歷fr,直到找到匹配的名稱。找到該名稱後,使用類似的get方法打印出您剛剛找到的匹配Friendly所需的所有信息。

0

這應該工作:

public List<Friendlies> search(String name) { 

    List<Friendlies> list = new ArrayList<Friendlies>(); 
    for(Friendlies friendlies : fr) { 
     if(friendlies.getName().equals(name)) { 
      list.add(friendlies); 
     } 
    } 

    return list; 

} 

然後,返回列表,實現數據:)

0

假設AccessFriendlyFile的一個不錯的顯示將數據加載到您的陣列,您可以使用for每次循環,如果你想retieve所有匹配的名字:

List<Friendlies> getByName(String searched){ 
    List<Friendlies> result = new Arraylist<Friendlies>(); 
    for (Friendlies currentFriendly : fr){ 
     if (searched.equalsIgnoreCase(currentFriendly.getName()){ 
     result.add(currentFriendly); 
     } 
    } 
    return result; 
} 

只有第一種:

Friendlies getByName(String searched){ 
    for (Friendlies currentFriendly : fr){ 
     if (searched.equalsIgnoreCase(currentFriendly.getName()){ 
     return currentFriendly; 
     } 
    } 
    return null; 
} 

您應該使用列表而不是固定數組。如果文件包含超過100條記錄,您將得到一個indexoutofbounds異常。

相關問題