簡體   English   中英

需要幫助將文本文件讀入Object

[英]Need help reading text file into Object

我創建了一個Person類,其中包含參數firstname,lastname和year。 我需要弄清楚如何從文本文件中讀取輸入並創建一個人員數組。 這是我正在嘗試的,基於我從網絡匯集的不同來源。

String name = "People.txt";
        Person[] people = new ArrayList<Person>();
        BufferedReader br = new BufferedReader(new FileReader(name));
        String line = br.readLine();
        while(line != null)
        {
            people.add(line);
            line = br.readLine();
        }

但當然,這不起作用。 我懷疑它甚至接近正確,所以我想知道你們是否有任何建議。

另外,我的Person類的構造函數如下:

public Person(String firstName, String lastName, int age){
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }



The error reads,

`SortingImplementation.java:15: incompatible types
found   : java.util.ArrayList<Person>
required: Person[]
                Person[] people = new ArrayList<Person>();
                                  ^
SortingImplementation.java:20: cannot find symbol
symbol  : method add(java.lang.String)
location: class Person[]
                        people.add(line);
                              ^
Note: SortingMethod.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
2 errors
`

文本文件是

Larry Kim 45
Bob Stuart 51
Nancy Davis 38
John Doe   49
Henry Miles 23
Albert Lee 36
Mary Wing  43
Tony Rich  55
Ally Sneetch  19
Carrie Chrome 77
David Abkenzy 41
Young Old    18
Snow White  70
George Herald 60
Mike Bush 22
Peter Paul 33
Peter Pan 44
Mary Paul 25
Ray Romano 55

好吧,甜蜜。 你們真棒,也許今晚我會入睡。 連續兩天試圖完成這項任務,開始產生幻覺。 我遇到的最后一個問題,我確信它很簡單,但我收到了這個錯誤。

SortingImplementation.java:15: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
                BufferedReader br = new BufferedReader(new FileReader("People.txt"));
                                                       ^
SortingImplementation.java:16: unreported exception java.io.IOException; must be caught or declared to be thrown
                String line = br.readLine();

我認為它可能只與我實例化的方式有關,或者我可能沒有導入正確的頭文件。 這是我導入的內容。

import javax.swing.JOptionPane;
import java.util.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.io.*;
import java.nio.*;

你們看到任何可能遺漏的東西嗎?

你有兩個選擇。

  1. 在不更改Person類的情況下,在讀取該行之后,您可以將數據解析為firstNamelastNameage ,並將這些參數發送給構造函數。
  2. 您可以向Person添加一個構造函數,該構造函數需要單個未解析的String參數,並在構造函數中執行所有解析並從中分配初始化變量。

有關如何執行任一選項的詳細信息,您必須向我們展示文本文件的確切內容。


但是目前,您的問題是您正在嘗試將String對象添加到Person對象的ArrayList中。

你需要這樣做:

people.add(new Person(/*constructor arguments*/));

現在您已更新為包含文件格式,請嘗試以下操作:

while(line != null) {
    String[] lineParts = line.split("\\s+");
    people.add(new Person(lineParts[0],lineParts[1],parseInt(lineParts[2]));
    line = br.readLine();
}

通過額外的Person構造函數進行操作沒有太大區別。

Person類中,添加以下構造函數:

public Person(String s) {
    String[] parts = s.split("\\s+");
    this(parts[0], parts[1], parseInt(parts[2]));
}

然后在main中,讓你的while循環看起來像這樣:

while(line != null) {
    people.add(new Person(line));
    line = br.readLine();
}

你不能只是添加一個行String並期望它神奇地轉換為一個Person 您需要將每一行拆分為單獨的String然后從這些String創建一個人

    Person[] people = new ArrayList<Person>();  
    BufferedReader br = new BufferedReader(new FileReader(name));
    String line = br.readLine();
    while(line != null)
    {
        people.add(line);
        line = br.readLine();
    }

你需要做這樣的事情,注意從數組到ArrayList<Person>

    ArrayList<Person> people = new ArrayList<Person>();  // Notice change here
    BufferedReader br = new BufferedReader(new FileReader(name));
    String line;
    while((line = br.nextLine()) != null)
    {
        String[] tokens = line.split("\\s+");
        String firstName = tokens[0];
        String lastName = tokens[1];
        int age = Integer.parseInt(tokens[2]);  // parse String to int

        people.add(new Person(firstName, lastName, age));
    }

這假設輸入文本的格式如下

FirstName LastName年齡

另外,確保將所有內容都包裝在try/catch塊中,或者讓方法throws IOException ;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM