简体   繁体   English

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

[英]Getting parameters from a text file using Java String.split method

I'm working on a school project and I'm not sure how to proceed. 我正在做一个学校项目,不确定如何进行。 I have the following text file: 我有以下文本文件:

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

I know how to read the text file with the Scanner class. 我知道如何使用Scanner类读取文本文件。 I know in the main method that I'm supposed to have a String.split method to read comma separated values. 我知道在main方法中,我应该有一个String.split方法来读取逗号分隔的值。 I know how to use the comma as the delimiter but I'm not sure how to pass what I'm reading into the appropriate constructor. 我知道如何使用逗号作为定界符,但是我不确定如何将正在读取的内容传递给适当的构造函数。 Assume all variables are declared. 假设所有变量都已声明。

This is the super constructor 这是超级构造函数

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

this is the Auto class (12 parameters) 这是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);
    }
}

and Property class (11 parameters) 和Property类(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);
    }
}

How is it that I pass the values scanned from the text file into their proper constructors? 我如何将从文本文件扫描的值传递到其适当的构造函数中?

String#split() method returns String[] . String#split()方法返回String[] Now simply get it from the array using index. 现在,只需使用索引从数组中获取它即可。

sample code: 样例代码:

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

Based on what the somewhat hint-like comments , I've come up with this. 基于一些类似提示的评论 ,我提出了这个建议。

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM