0

我创建了一个 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.*;

你们有没有看到什么可能丢失的东西?

4

2 回答 2

1

你有两个选择。

  1. 在不更改Person类的情况下,阅读该行后,您可以将数据解析为firstNamelastNameage,并将这些参数发送给构造函数。
  2. 您可以添加一个构造函数,该构造函数Person需要一个未解析String的参数,并在构造函数中进行所有解析,并从那里分配初始化变量。

有关如何执行任一选项的详细信息,您必须向我们展示文本文件的确切外观。


但是目前,您的问题是您正在尝试将String对象添加到ArrayList对象中Person

你需要这样做:

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();
}
于 2013-11-15T02:24:06.520 回答
1

您不能只添加一行String并期望它神奇地转换为Person. 您需要将每一行分成单独String的 s 然后从这些中创建一个人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));
    }

这是假设输入文本的格式如下

名字 姓 年龄

另外,请确保将所有内容包装在一个try/catch块中,或者使用方法throws IOException

于 2013-11-15T02:24:22.613 回答