0

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?

ReiHinoX
  • 51
  • 1
  • 13
  • My question is how to read the text file which has different length parameter lists...this is supposed to be a demonstration of inheritance. But I'm not sure how to put everything from the array into the proper constructor. There are seven different lines. – ReiHinoX Jul 12 '14 at 20:44
  • 1
    You should also realize that your question is asking for too many things at once...you want to figure out how to pass parameters to your constructors, but you ***also*** want to know how to read a text file. Many users would close your question as Too Broad because of that. –  Jul 13 '14 at 01:52
  • What the heck is a lambda method???? Also, I know how to read from a text file, but i'm not sure what to _do_ with the data I've just read...I'm not sure how to make sure it goes to its proper constructor. – ReiHinoX Jul 13 '14 at 01:54
  • @ReiHinoX don't worry about lambda methods...you should learn them eventually if you're going to make a career out of software development, but for a school project, don't worry about it for now. Finally, if you already know how to read the text file, **then you should update your question to ask what your real problem is**, because right now you're just kind of wasting other people's time by asking for help solving things that you don't really need help solving. –  Jul 13 '14 at 02:10
  • I know how to read a basic text file. But with all these lines being different lengths, it's different. – ReiHinoX Jul 13 '14 at 02:11
  • @ReiHinoX then why are you still asking for help reading a text file at the end? –  Jul 13 '14 at 02:11
  • 1
    Because I don't know what to do with it _after_ I've used the Scanner object. – ReiHinoX Jul 13 '14 at 02:12
  • And give me a break. I'm a little bit autistic. And i'm using java 7 because it's what the school computers run. – ReiHinoX Jul 13 '14 at 02:26
  • 2
    *But with all these lines being different lengths, it's different.* Some of the lines have 3 values, some have 11, some have 12. In your question, you provide three constructors, each taking different numbers of arguments. Is there same way you can use these facts together? – Carl Veazey Jul 13 '14 at 05:06
  • 1
    Should I use if statements to pass to the constructors, thenadd them to an arraylist of type ? – ReiHinoX Jul 13 '14 at 13:38
  • Sounds like a solid approach, what happened when you tried it? – Carl Veazey Jul 13 '14 at 15:30
  • I'm not sure its what my teacher wants but it works – ReiHinoX Jul 13 '14 at 16:39

2 Answers2

2

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]);
Braj
  • 46,415
  • 5
  • 60
  • 76
2

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();
Community
  • 1
  • 1
ReiHinoX
  • 51
  • 1
  • 13