使用Java String.split方法从文本文件中获取参数

时间:2014-07-12 19:53:05

标签: java string text parameters split

我正在做一个学校项目,我不知道该怎么办。我有以下文本文件:

08-7123,01/20/1990,3000
08-6325,03/15/2000,1000,8765252,Honda,Civic,2009,Bob Jones,15 Price St.,Oxford,MS,0
08-9867,06/20/2010,4000,1500,1999,Sally Fields,60 William Dr.,Tupelo,MS,false,true
03-1653,07/09/2012,8000,12000,2012,Mike Macias,314 Circular Cir.,Seattle,WA,true,true
08-9831,10/10/1986,600,8008135,Delorean,DCM-12,1981,Calvin Klein,1432 Rich St.,Hill Valley,CA,3
08-3467,12/12/2001,1250,1750,2001,Jessica Johnson,74 Jefferson Ave.,Oxford,MS,false,false
08-5614,04/25/2011,825,7765224,Ford,F-150,2011,Bill Buckner,12 Bramlett Blvd.,Sardis,MS,2

我知道如何使用Scanner类读取文本文件。我知道在main方法中,我应该使用String.split方法来读取逗号分隔值。我知道如何使用逗号作为分隔符,但我不确定如何将我正在阅读的内容传递给相应的构造函数。假设声明了所有变量。

这是超级构造函数

public Insurance(String pNum, String pDate, int yPrem)
{
    this.pNum = pNum;
    this.pDate = pDate;
    this.yPrem = yPrem;
}

这是Auto类(12个参数)

public class Auto extends Insurance
{
    public Auto(String pNum, String pDate, int yPrem,
        String vehicleID, String make, String model,
        int year, String name, String address,
        String city, String state, int accidents)
    {
        super(pNum, pDate, yPrem);
        this.vehicleID = vehicleID;
        this.make = make;
        his.model = model;
        this.year = year;
        this.accidents = accidents;
        age = 2014-year;
        owner = new Owner(name, address, city, state);
    }
}

和Property class(11个参数)

public class Property extends Insurance
{
    private int sqft, yrBuilt;
    private boolean fireStation, gated;
    Owner owner;

    public Property(String pNum, String pDate,
        int yPrem, int sqft, int yrBuilt, String name,
        String address, String city, String state,
        boolean fireStation, boolean gated)
    {
        super(pNum, pDate, yPrem);
        this.sqft = sqft;
        this.yrBuilt = yrBuilt;
        this.fireStation = fireStation;
        this.gated = gated;
        owner = new Owner(name, address, city, state);
    }
}

我如何将从文本文件中扫描的值传递给正确的构造函数?

2 个答案:

答案 0 :(得分:2)

String#split()方法返回String[]。现在只需使用索引从数组中获取它。

示例代码:

String[] array = str.split(",");
String pNum = array[0];
String pDate = array[1];
int yPrem = Integer.parseInt(array[2]);
...
boolean gated = Boolean.valueOf(array[11]);

答案 1 :(得分:2)

根据有些hint-like comments,我想出了这个。

ArrayList<Insurance> policies = new ArrayList<Insurance>();
while (fileScan.hasNext())
{
    String[] array = fileScan.nextLine().split(",");
    if (array.length == 3)
    {
        policies.add(new Insurance(array[0],array[1], Integer.parseInt(array[2])));
    }
    else if (array.length == 11)
    {
        policies.add(new Property(array[0] , array[1], Integer.parseInt(array[2]),
            Integer.parseInt(array[3]), Integer.parseInt(array[4]), array[5],
            array[6], array[7], array[8], Boolean.valueOf(array[9]),
            Boolean.valueOf(array[10])));
    }
    else if (array.length == 12)
    {
        policies.add(new Auto(array[0],array[1], Integer.parseInt(array[2]),
        array[3], array[4], array[5], Integer.parseInt(array[6]), array[7],
        array[8], array[9], array[10], Integer.parseInt(array[11])));
    }

}
fileScan.close();