Hello, im having trouble with getting this program I am writing for one of my courses to run. The program has a 4 different classes. I am having trouble getting the program to read input from a file, having it take those items and creating vehicle objects, and then putting those vehicle objects into an array. The code I am providing below is only part of the program but should be enough to help me with the problem im having so far.
the file it is reading from(fleetCars.txt) contains this information. There is a tab between each item for each car.
Hyundai Sonata 2010 ABC236347NM2N2NW2 18455.34 8765 7567
Chevy Blazer 1998 1234H32343LMN3423 29556.65 38559 38559
public class Fleet { Vehicle[] fleetVehicles = new Vehicle[4]; public Fleet() { super(); } public Fleet(String fileName) throws IOException { Scanner inputFile = new Scanner(fileName); for (int i = 0; i < fleetVehicles.length; i++) { Vehicle vehobj = new Vehicle(); vehobj.readRecord(inputFile); fleetVehicles[i] = vehobj; } } public String toString() { Vehicle obj = new Vehicle(); for (int i = 0; i < fleetVehicles.length; i++) { fleetVehicles[i] = obj; return obj.toString(); } return obj.toString(); }
public class Vehicle { private String vin; private String make; private String model; private int year; private double value; private int milesDriven; private int lastOilChange; public Vehicle() { super(); } public Vehicle(String vin, String make, String model, int year, double value, int milesDriven, int lastOilChange) { this(vin, make, model, year, value); this.milesDriven = milesDriven; this.lastOilChange = lastOilChange; } public String toString() { return "Make:\t\t" + this.make + "\nModel:\t\t" + this.model + "\nYear:\t\t" + this.year + "\nValue:\t\t" + this.value + "\nVIN:\t\t" + this.getVIN() + "\nMiles Driven: " + this.milesDriven + "\nNext oil change at " + this.getMilesToNextOilChange() + " miles"; } public void readRecord(Scanner inputFile) throws IOException { make = inputFile.next(); model = inputFile.next(); year = inputFile.nextInt(); vin = inputFile.next(); value = inputFile.nextInt(); milesDriven = inputFile.nextInt(); lastOilChange = inputFile.nextInt(); }
The last bit of code is the part that runs the whole program. This part asks the user for the name of the file. It then is supposed to open the file and use it to build the fleet object.
public class FleetInterfaceApp { private Fleet fleet; private Menu menu; private void createMenu() { String[] choices = { "Add a vehicle", "Delete a vehicle", "Update miles driven", "Record oil change", "Print listing of all fleet vehicles", "Print listing of all vehicles to be replaced", "Print listing of all vehicles that need and oil change", "Quit" }; menu = new Menu(choices); } public void run() throws IOException { Scanner keyboard = new Scanner(System.in); System.out .println("What is the name of the file containing the fleet vehicle information?"); String fileName = keyboard.nextLine(); File file = new File(fileName); Scanner inputFile = new Scanner(file); Fleet fleet = new Fleet(fileName); int choice; this.createMenu(); choice = this.menu.getChoice(); System.out.println("You entered choice " + choice); if (choice == 1) choice1(); if (choice == 2) choice2(); if (choice == 3) choice3(); if (choice == 4) choice4(); if (choice == 5) choice5(); if (choice == 6) choice6(); if (choice == 7) choice7(); }
This is the error I get when I run fleetInterFaceApp
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:838)
at java.util.Scanner.next(Scanner.java:1347)
at Vehicle.readRecord(Vehicle.java:285)
at Fleet.<init>(Fleet.java:51)
at FleetInterfaceApp.run(FleetInterfaceApp.java:47)
at FleetInterfaceApp.main(FleetInterfaceApp.java:166)
Thanks in advance for any help!