2012-05-02 34 views
-3

我需要從一個文件中顯示更多的愛好,我不知道該怎麼做。我需要的是這樣的:Stan Ilie : baschet, programare如何爲一個人添加更多的愛好

我的代碼在這裏:

import java.io.*; 
import java.util.*; 

public class ListaHobby 
{ 
    String line = ""; 
    Hobby h, h1; 
    Persoana p; 
    BufferedWriter bw = null; 
    ArrayList<Persoana> listOfPersons; 
    ArrayList<Hobby> listOfHobbies; 

    public void writeListaHobbies() 
    { 
     try 
     { 
      listOfPersons = new ArrayList<Persoana>(); 
      FileReader file1 = new FileReader("Persoane.txt"); 

      listOfHobbies = new ArrayList<Hobby>(); 
      FileReader file2 = new FileReader("Hobby.txt"); 

      BufferedReader br1 = new BufferedReader(file1); 
      BufferedReader br2 = new BufferedReader(file2); 
      String line1 = ""; 
      String line2 = ""; 

      while ((line1 = br1.readLine()) != null) 
      { 
       if (!line1.trim().contains("ID")) 
       { 
        String[] attributes = line1.split(";"); // split it at every ";" 

        p = new Persoana(); // make a new person 
        p.setId(Integer.parseInt(attributes[0])); 
        p.setNume(attributes[1]); 
        p.setPrenume(attributes[2]); 
        p.setDataNasterii(attributes[3]); 
        p.setProfesie(attributes[4]); 

        listOfPersons.add(p); 
       } 
      } 

      while ((line2 = br2.readLine()) != null) 
      { 
       if (!line2.trim().contains("ID")) 
       { 
        String[] attributes = line2.split(";"); // split it at every ";" 

        h = new Hobby(); // make a new hobby 
        h.setId(Integer.parseInt(attributes[0])); 
        h.setNume(attributes[1]); 
        h.setDescriere(attributes[2]); 
        h.setNrPers(Integer.parseInt(attributes[3])); 
        h.setElemNecesar(attributes[4]); 

        listOfHobbies.add(h); 
       } 
      } 

      FileWriter fw = new FileWriter("PersHobby.txt"); 
      bw = new BufferedWriter(fw); 

      for (Persoana p : listOfPersons) 
      { 
       for (Hobby h : listOfHobbies) 
       { 
        String s = p.getNume() + " " + p.getPrenume() + ": "; 

        String s1 = h.getNume(); 

        System.out.print(s); 
        System.out.println(s1); 

        bw.write(s); 
        bw.append(s1); 
        bw.newLine(); 
       } 
      } 

      bw.close(); 
     } 
     catch (IOException ex) 
     { 
      System.out.println("Error opening file."); 
      System.exit(1); 
     } 
    } 
} 

和我的輸出是這樣的:

Stan Ilie: baschet 
    Stan Ilie: fotbal 
    Stan Ilie: chitara 
    Stan Ilie: pianul 
    Stan Ilie: programarea 
    Becali GG: baschet 
    Becali GG: fotbal 
    Becali GG: chitara 
    Becali GG: pianul 
    Becali GG: programarea ..... 
+3

這是功課? – Bernard

+0

請爲代碼塊使用一致的邏輯縮進。 –

+0

是的,我需要這樣做直到明天 – Laura

回答

2

因爲這個被標記爲功課,我不想給你的解決方案,但我給你一個提示:

問題是你每次讀一個愛好時寫輸出文件的一行。 您必須創建一個字符串,其中包含所有與個人有關的業餘愛好,然後才能將此字符串寫入該文件。

僞代碼:

foreach Person p { 
    String currentPerson = p.getNume() + " " + p.getPrenume() + ": "; 

    foreach Hobby h { 
     currentPerson += h.getNume() + ","; 
    } 

    Write currentPerson to file and print currentPerson to console 
} 
+0

感謝您的幫助。 – Laura

+0

如果它幫助你,請將我的答案標記爲已接受:) – 2012-05-02 19:17:57

+0

+1用於編寫僞代碼而不是共享原始代碼。 – Chad

相關問題