I need to read in data from a text file with a scanner, which contains a list of vehicles. I'm then adding these Vehicles to an ArrayList. Each we have separate subclasses already written for a Car, Bike, raceCar, and Vehicle class. Each vehicle type is identified by a single char. (ex. raceCar 'R', Bike 'B' etc). So if I'm scanning from the file and I reach an 'R' that means that I have to scan the next 5 lines following it for (Make, Model, numWheels, engineSize, raceType). However, if I scan char 'C' or 'B' i'll have to read only the next 4.
I keep getting this error:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(String.java:658)
at VehicleDriver.inputVehicles(VehicleDriver.java:18)
at VehicleDriver.main(VehicleDriver.java:9)
--------------------------
From here i'm not sure what i'm supposed to do. And honestly i'm not even sure if this code I have will work and preform the necessary task. Any tips on what I could do?
import java.util.*; import java.io.*;//File public class VehicleDriver { public static void main (String [] args)throws IOException { ArrayList <Vehicle> vehicles = new ArrayList<Vehicle> (); inputVehicles(vehicles); }//end main() public static void inputVehicles(ArrayList<Vehicle> vehicles)throws IOException { File infile = new File("vehicles.txt"); Scanner scan = new Scanner(infile); String x = ""; char c = x.charAt(0); String make = ""; String model = ""; int numWheels = 0; int numGears = 0; String engineSize = ""; String raceType = ""; if(!infile.exists()) { System.out.println("Your file does not exist"); System.exit(0); }//end if while(scan.hasNext()) { if(c == 'R') { make = scan.nextLine(); model = scan.nextLine(); numWheels = scan.nextInt(); engineSize = scan.nextLine(); raceType = scan.nextLine(); RaceCar rc = new RaceCar(make, model, numWheels, engineSize, raceType); vehicles.add(rc); System.out.println(rc); } else if(c == 'B') { make = scan.nextLine(); model = scan.nextLine(); numWheels = scan.nextInt(); numGears = scan.nextInt(); Bike bi = new Bike(make, model, numWheels, numGears); vehicles.add(bi); System.out.println(bi); } else if(c == 'C') { make = scan.nextLine(); model = scan.nextLine(); numWheels = scan.nextInt(); engineSize = scan.nextLine(); Car ca = new Car(make, model, numWheels, engineSize); vehicles.add(ca); System.out.println(ca); } }//end forloop scan.close(); }//end inputVehicles }//end class
Below is the textfile
R Dodge Charger 4 5.7L Nascar C Volkswagon Taureg 4 V6 B Schwinn Super Deluxe 2 2 R Aston Martin DB9 4 6.0L Le Mans B Cannondale Trigger 29 3 2 30 C Ford Focus 4 2.0L
Vehicle Class
public class Vehicle { private String make; private String model; protected int numWheels; public Vehicle() { make = model = ""; numWheels = 0; }//end default public Vehicle(String make, String model, int numWheels) { this.make = make; this.model = model; this.numWheels = numWheels; }//end 2nd constructor public String getMake() { return make; }//end getMake() public String getModel() { return model; }//end getModel() public int getNumWheels() { return numWheels; }//end getNumWheels() //setters go here public String toString() { return "Make: " + make + "\nModel: " + model + "\nNumber of Wheels: " + numWheels; }//end toString() }//end class Vehicle
Car Class
public class Car extends Vehicle { private String engineSize; public Car() { super(); engineSize = ""; }//end super constructor public Car(String make, String model, int numWheels, String engineSize) { super(make, model, numWheels); this.engineSize = engineSize; } public String getEngineSize() { return engineSize; } public String toString() { String str = ""; str += super.toString(); str += "\nEngine size: " + engineSize; return str; } }
RaceCar Class
public class RaceCar extends Car { private String raceType; public RaceCar() { super();//default constructor of Car raceType = ""; }//end super constructor public RaceCar(String make, String model, int numWheels, String engineSize, String raceType) { super(make, model, numWheels, engineSize); this.raceType = raceType; }//end 2nd constructor public String getRaceType() { return raceType; } public String toString() { String str = ""; str += super.toString(); str += "\nRace Type: " + raceType; return str; } }
Bike Class
public class Bike extends Vehicle { private int numGears; public Bike() { super(); numGears = 0; }//end super constructor public Bike(String make, String model, int numWheels, int numGears) { super(make, model, numWheels); this.numGears = numGears; } public int getNumGears() { return numGears; }//end getNumGears() public String toString() { String str = ""; str += super.toString(); str += "\nNumber Of Gears: " + numGears; return str; } }