简体   繁体   中英

Reading a text file line by line and storing an object in the array

The code I've currently created stores the first line of the text file, creates a new Vehicle object and puts it in the array at the first position of null, and stores the same line in every null value in the array. I need it to be able to:

Store the contents of the first line, then store a new Vehicle object in the first place in the array that is null. Then repeat until there are no more lines.

I believe it is a problem with my for loop. Note - I am required to use Array instead of ArrayList

public void addVehicle(Vehicle[] Honda) throws FileNotFoundException
{

        if(canAddVehicle() == true)
        {
        for(int i = 0; i < vehicles.length; i++)
        {
            if(vehicles[i] == null)
            {
                Scanner reader = new Scanner(file);

                Honda[i] = new Vehicle();
                Honda[i].readRecord(reader);
                vehicles[i] = Honda[i];

                reader.close();
            }
        }
            System.out.println("Vehicle Added!");
        }
        else
        {
            System.out.println("You can not add more than 4 vehicles.");
        }
}

Vehicle class:

public void readRecord(Scanner reader)
{

    setMake(reader.next());
    setModel(reader.next());
    setYear(reader.nextInt());
    setvin(reader.next());
    setValue(reader.nextDouble());
    setMilesDriven(reader.nextInt());
    setLastOilChange(reader.nextInt());



}

Data file:

Hyundai Sonata  2010    ABC236347NM2N2NW2   18455.34    8765    7567
Chevy   Blazer  1998    1234H32343LMN3423   29556.65    38559   38559

//EDIT\\ Constraits : I cannot create any new public methods or constructors, and I cannot have any additional class level data

You're looping within the readRecord method, even though that's meant to only store one object, isn't it?

It's possible that you can just remove the while loop - although that then relies on the addVehicle caller knowing how many entries are in the file.

It seems more likely that you should have a method to read everything from a file, populating a List<Vehicle> and returning it. For example:

public List<Vehicle> readVehicles(String file)
{
    Scanner reader = new Scanner(file);

    List<Vehicle> vehicles = new ArrayList<Vehicle>();
    try
    {
        while (reader.hasNextLine())
        {
            vehicles.add(Vehicle.readFromScanner(reader));
        } 
    }
    finally
    {
        reader.close();
    }
    return vehicles;
}

// In vehicle
public static Vehicle readFromScanner(Scanner scanner)
{
    String make = reader.next();
    String model = reader.next();
    int year = reader.nextInt();
    String vin = reader.next();
    // Don't use double for currency values
    BigDecimal value = reader.nextBigDecimal();
    int milesDriven = reader.nextInt();
    // Shouldn't this be some sort of date type?
    int lastOilChange = reader.nextInt();

    // I'll assume you have a constructor like this
    return new Vehicle(make, model, year, vin, value, milesDriven,
                       lastOilChange);
}

Found my solution!

public boolean addVehicle(Vehicle[] Honda) throws FileNotFoundException
{
    boolean found = false;
    int position = 0;
        if(canAddVehicle() == true)
        {
            for(int i = 0; i < vehicles.length && !found; i++)
            {
                if(vehicles[i] == null)
                {
                    position = i;
                    found = true;
                }
            }

               Scanner reader = new Scanner(file);
               while(reader.hasNext())
               {
                   Honda[position] = new Vehicle();
                   Honda[position].readRecord(reader);
                   vehicles[position] = Honda[position];
                   position++;

               }
                reader.close();
                return true;
        }
        return false;
}

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