简体   繁体   中英

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. I know in the main method that I'm supposed to have a String.split method to read comma separated values. 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)

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)

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[] . 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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