2012-04-11 5 views
0

person.txtには、その人の詳細が格納されています。 そうだね。ファイルリーダーが最後まですべてのデータを読み取らない

John 
Smith 
aösldkjf 
5 
8645 
asdfasf 
0441234545 
++++++ 
Adam 
Gilchrist 
ads 
asf 
asf 
asfd 
0441234546 
++++++ 

次に、このファイルからデータを読み取るFileManagerクラスを作成しました。 2つの異なるエントリがあることを示します。 しかし、それは常に最初の8行を読み、移動しません。そのため、最初の人物(例: - John Smith)はAddressBookという名前の "LinkedList"に2回追加されます。クラスPerson

public void addFilePerson(LinkedList<String> list){ 

    vorname = list.get(0); 
    nachname = list.get(1); 
    strasse = list.get(2); 
    hausnummer = list.get(3); 
    plz = list.get(4); 
    telefon = list.get(5); 
    wohnort = list.get(6); 
} 

答えて

1

あなたはそれに追加することを繰り返し LinkedList<String>を作成するとしている中

//ファイルマネージャクラス

public class FileManager { 

    public static void readFile() { 

     Scanner x; 

     LinkedList<String> tempList = new LinkedList<String>(); 

     try { 
      x = new Scanner(new File("Person.txt")); 

      @SuppressWarnings("unused") 
      String temp = null; 

      while (x.hasNext()) { 
       tempList.add(x.next()); 
       tempList.add(x.next()); 
       tempList.add(x.next()); 
       tempList.add(x.next()); 
       tempList.add(x.next()); 
       tempList.add(x.next()); 
       tempList.add(x.next()); 
       tempList.add(x.next()); 

       Person person = new Person(); 

       person.addFilePerson(tempList); 

       Main.addressBook.add(person); 

      } 
     } catch (Exception e) { 
      System.out.println("could't find the file"); 
     } 
    } 
} 

// addFilePerson方法。この行を移動する:

LinkedList<String> tempList = new LinkedList<String>(); 

into whileループ。代わりに - そして好ましくは、IMO - 異なる部分に対して個別のプロパティを使用します。

// TODO: Consider what happens if the file runs out half way through a person... 
while (x.hasNext()) { 
    Person person = new Person(); 
    person.setFirstName(x.next()); 
    person.setLastName(x.next()); 
    person.setStreet(x.next()); 
    person.setTown(x.next()); 
    person.setTelephoneNumber(x.next()); 
    person.setCity(x.next()); // Or whatever... 

    Main.addressBook.add(person); 
} 

ありPersonのために、「ビルダー」タイプを作成し、Person自体は不変作るの周りに他のオプションがあり、あなたは別のAddressを作成することができますタイプ...

1

ファイルから人物を読む間にリストをクリア(または再作成)する必要があります。それ以外の場合は、同じ人物(最初に読んだ人物)をアドレス帳に追加し続けます。だから、Jonの提案どおり、毎回ループ内で一時リストを作成するか、各ラウンド後に消去してください。

 while (x.hasNext()) { 
      tempList.add(x.next()); 
      ... 

      Main.addressBook.add(person); 

      tempList.clear(); 
     } 
+0

ちょうど1行をやったほうが良いと思います。完璧に動作します。 –

1

実際に移動します。このライン:

person.addFilePerson(tempList); 

あなたがパラメータとしてtempListを送るが、addFilePerson方法であなたはいつもtempList初の7つの項目をお読みください。ループの繰り返しごとにtempListをクリアする必要があります。

0

next()およびhasNext()の代わりにnextLine()およびhasNextLine()を使用する必要があります。スキャナはコンテキストを認識しているため、デフォルトのトークン読み取り動作は行ベースではない可能性があります。

0

あなたは、コードの

Person person = new Person(); 
Address address = new Address(); 
person.setAddress(address); 

person.setFirstName(x.next()); 
person.setLastName(x.next()); 
address.setStreetName(x.next()); 
address.setHouseNumber(x.next()); 
address.setZipCode(x.next()); 
person.setPhoneNumber(x.next()); 
address.setCityName(x.next()); 
関連する問題