从.txt文件中读取数据并创建对象数组

时间:2018-12-02 02:50:49

标签: java arrays

请给我一些帮助:我正在用Java模拟飞行花名册。名册将容纳25名乘客,其中22名来自文本文件(PassengerList.txt)。对于每个乘客,有3个必需的数据点;名称,座位类别和座位编号以及2个可选数据点常旅客号码和常旅客积分。每个乘客都在自己的线路上,每个数据点都用逗号分隔。例如:

Allen George,Economy Class,8A,#GEO456,10000
Judy Hellman,Economy Class,8B

到目前为止,我拥有此类以及构造函数:

public class Passengers
{

  private String name, type, seat, flyernum;
  private int points;

  //Constructor to intialize the instance data
  Passengers(String full_name, String seat_type, String seat_number,
  String frequent_flyer_number, int frequent_flyer_points) 
  { 
    name=full_name;
    type=seat_type;
    seat=seat_number;
    flyernum=frequent_flyer_number;
    points=frequent_flyer_points;
  } //end Passengers 

我需要做的是从文本文件中读取每一行并创建数组,即,使第一行看起来像这样:

Passenger passenger1 = new Passenger ("Allen George","Economy Class","8A" 
,"#GEO456",10000)

进入这样的数组:

Passenger[0] = passenger1;

我显然是Java初学者,但是我已经被这个问题困扰了很长时间了,当我尝试一些新的东西时,我不断收到错误消息。我一直在使用Scanner读取文件。文本文件不需要被覆盖,只需由程序读取和扫描即可。也只能使用数组,ArrayList是不行的。也只有两个文件,Passengers类和main方法。请帮忙!谢谢!

2 个答案:

答案 0 :(得分:0)

您可以像下面这样:

首先,您需要一个Passenger.class。看起来像这样(请注意,我已经添加了toString():

public class Passenger {

private String name, type, seat, flyernum;
private int points;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public String getSeat() {
    return seat;
}

public void setSeat(String seat) {
    this.seat = seat;
}

public void setFlyernum(String flyernum) {
    this.flyernum = flyernum;
}

public int getPoints() {
    return points;
}

public void setPoints(int points) {
    this.points = points;
}

@Override
public String toString() {
    return "Passenger{" +
            "name='" + name + '\'' +
            ", type='" + type + '\'' +
            ", seat='" + seat + '\'' +
            ", flyernum='" + flyernum + '\'' +
            ", points=" + points +
            '}';
}

}

现在为了从文件中获取乘客的详细信息,我创建了GetPassengerDetails.class,它处理从CSV文件中读取数据并为每个乘客分配正确的值。

public class GetPassengerDetails{
/** Reads the file one line at a time. Each line will is that split up and translated into a Passenger object */
public List<Passenger> getPassengersFromFile(BufferedReader reader) throws IOException {
    List<Passenger> passengers = new ArrayList<>();

    String line;
    while ((line = reader.readLine()) != null) {
        String[] passengerDetails = line.trim().split(",");
        Passenger passenger = new Passenger();
        for (int i = 0; i < passengerDetails.length; i++) {

            SetPassengerName(passengerDetails, passenger, i);

            setPassengerFlightType(passengerDetails, passenger, i);

            setPassengerSeatNumber(passengerDetails, passenger, i);

            SetPassengerFlyerNumber(passengerDetails, passenger, i);
            setPassengerPoints(passengerDetails, passenger, i);
        }

        passengers.add(passenger);

    }

    return passengers;
}

private void setPassengerPoints(String[] passengerDetails, Passenger passenger, int i) {
    if(i< passengerDetails.length && i == 4) {
        passenger.setPoints(Integer.parseInt(String.valueOf(passengerDetails[4])));
    }
}

private void SetPassengerFlyerNumber(String[] passengerDetails, Passenger passenger, int i) {
    if(i< passengerDetails.length && i == 3) {
        passenger.setFlyernum(String.valueOf(passengerDetails[3]));
    }
}

private void setPassengerSeatNumber(String[] passengerDetails, Passenger passenger, int i) {
    if(i< passengerDetails.length && i == 2) {
        passenger.setSeat(String.valueOf(passengerDetails[2]));
    }
}

private void setPassengerFlightType(String[] passengerDetails, Passenger passenger, int i) {
    if(i< passengerDetails.length && i == 1) {
        passenger.setType(String.valueOf(passengerDetails[1]));
    }
}

private void SetPassengerName(String[] passengerDetails, Passenger passenger, int i) {
    if(i< passengerDetails.length & i == 0) {
        passenger.setName(String.valueOf(passengerDetails[i]));
    }
}

}

这是测试以上代码的主要方法:

public class Main {

public static void main(String[] args) throws IOException {

      String fileName = "resources/passengers.csv";

    BufferedReader reader = new BufferedReader(new FileReader(fileName));
    GetPassengerDetails passengerDetails = new GetPassengerDetails();

    List<Passenger> passengers = passengerDetails.getPassengersFromFile(reader);

    // For Testing Purposes lets get the Passengers

    for (Passenger passenger : passengers
            ) {
        System.out.println(passenger.toString());
    }
  }
}   

运行main方法后,将得到以下结果:

enter image description here

答案 1 :(得分:0)

使用此主要方法从文本文件中读取数据,并将数据聚合到Passengers对象中。旅客清单objec中的旅客的全部清单。

public static void main(String[] args) {
    List<Passengers> passengersList = new ArrayList<Passengers>();
    File file = new File("Your file location..");
    try {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String st;
        while ((st = br.readLine()) != null){
            String[] data = st.split(",");
            String flyNumber = null;
            int flyPoints = 0;
            switch (data.length){
                case 4: flyNumber = data[3];
                    break;
                case 5: flyNumber = data[3];
                    flyPoints = Integer.valueOf(data[4]);
                    break;
            }
            Passengers passenger = new Passengers(data[0], data[1], data[2], flyNumber, flyPoints);
            passengersList.add(passenger);
        }
        System.out.println(passengersList.get(0));
    } catch (IOException e) {
        e.printStackTrace();
    }
}