I Have a main Vehicletest and I have a vehicle class that i've modified. Now i use FileInputStream to pull data from a "vehicle.txt" but i have no idea how to grab it.
p.s I tried posting the code but i'm forbidden (probably because i'm a new user)
--- Update ---
/*/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor.*/ /** * * @author Harebear */ class Vehicle { private String ownerName; private String address; private String phone; private String email; public Vehicle(String ownerName, String address, String phone, String email) { this.ownerName = ownerName; this.address= address; this.phone = phone; this.email = email; } @Override public String toString() { return String.format("%-15s %-20s %10s %-15s ",ownerName, address, phone, email); } Object getPhone() { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } } class Car extends Vehicle { private boolean convertible; private String color; public Car(String ownerName, String address, String phone, String email, boolean convertible, String color) { super(ownerName, address, phone, email); this.convertible = convertible; this.color = color; } public boolean getConvertible() { return convertible; } public void setConvertible(boolean convertible) { this.convertible = convertible; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } @Override public String toString() { return super.toString() + "\r\n" + "Convertible : " + convertible + "\r\n" + "Color : " + color; } } class AmericanCar extends Car { private boolean madeDetroit; private boolean unionShop; public AmericanCar(String ownerName, String address, String phone, String email, boolean convertible, String color, boolean madeDetroit, boolean unionShop) { super(ownerName, address, phone, email, convertible, color); this.madeDetroit = madeDetroit; this.unionShop = unionShop; } public boolean getMadeDetroit() { return madeDetroit; } public void setMadeDetroit(boolean madeDetroit) { this.madeDetroit = madeDetroit; } public boolean getUnionShop() { return unionShop; } public void setUnionShop(boolean unionShop) { this.unionShop = unionShop; } @Override public String toString() { return super.toString() + "\r\n" + "Made in Detroit : " + madeDetroit + "\r\n" + "Union shop : " + unionShop; } } class ForeignCar extends Car { private String manufacturerCountry; private float importDuty; public String getManufacturerCountry() { return manufacturerCountry; } public ForeignCar(String ownerName, String address, String phone, String email, boolean convertible, String color, String manufacturerCountry, float importDuty) { super(ownerName, address, phone, email, convertible, color); this.manufacturerCountry = manufacturerCountry; this.importDuty = importDuty; } public void setManufacturerCountry(String manufacturerCountry) { this.manufacturerCountry = manufacturerCountry; } public float getImportDuty() { return importDuty; } public void setImportDuty(float importDuty) { this.importDuty = importDuty; } @Override public String toString() { return super.toString() + "\r\n" + "Manufacturer country : " + manufacturerCountry + "\r\n" + "Import duty : " + importDuty; } } class Bicycle extends Vehicle { private int numberSpeeds; public Bicycle(String ownerName, String address, String phone, String email, int numberSpeeds) { super(ownerName, address, phone, email); this.numberSpeeds = numberSpeeds; } public int getNumberSpeeds() { return numberSpeeds; } public void setNumberSpeeds(int numberSpeeds) { this.numberSpeeds = numberSpeeds; } @Override public String toString() { return super.toString() + "\r\n" + "Number of speeds : " + numberSpeeds; } } class Truck extends Vehicle { private float numberTons; private float truckCost; private String datePurchased; public Truck(String ownerName, String address, String phone, String email, float numberTons, float truckCost, String datePurchased) { super(ownerName, address, phone, email); this.numberTons = numberTons; this.truckCost = truckCost; this.datePurchased = datePurchased; } public float getNumberTons() { return numberTons; } public void setNumberTons(float numberTons) { this.numberTons = numberTons; } public float getTruckCost() { return truckCost; } public void setTruckCost(float truckCost) { this.truckCost = truckCost; } public String getDatePurchased() { return datePurchased; } public void setDatePurchased(String datePurchased) { this.datePurchased = datePurchased; } @Override public String toString() { return super.toString() + "\r\n" + "Number of tons : " + numberTons + "\r\n" + "Truck cost : " + truckCost + "\r\n" + "Date purchased : " + datePurchased; } }
--- Update ---
// VehicleTest.java import java.io.*; import java.util.*; import static jdk.nashorn.internal.objects.NativeJava.typeName; public class VehicleTest { public static void main(String[] args) throws IOException { ArrayList<Vehicle> vehicles = new ArrayList<Vehicle>(); FileInputStream in = new FileInputStream("vehicles.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String line; String ownerName, address, phone, email, color, manufacturerCountry, datePurchased; boolean convertible, madeDetroit, unionShop; float importDuty, numberTons, truckCost; int numberSpeeds; line = br.readLine(); try { while(line != null) { ownerName = br.readLine(); address = br.readLine(); phone = br.readLine(); email = br.readLine(); if (line.equals("vehicle")) { boolean add = vehicles.add(new Vehicle(ownerName, address, phone, email)); } else if (line.equals("car")) { convertible = Boolean.parseBoolean(br.readLine()); color = br.readLine(); vehicles.add(new Car(ownerName, address, phone, email, convertible, color)); } else if (line.equals("american car")) { convertible = Boolean.parseBoolean(br.readLine()); color = br.readLine(); madeDetroit = Boolean.parseBoolean(br.readLine()); unionShop = Boolean.parseBoolean(br.readLine()); vehicles.add(new AmericanCar(ownerName, address, phone, email, convertible, color, madeDetroit, unionShop)); } else if (line.equals("foreign car")) { convertible = Boolean.parseBoolean(br.readLine()); color = br.readLine(); manufacturerCountry = br.readLine(); importDuty = Float.parseFloat(br.readLine()); vehicles.add(new ForeignCar(ownerName, address, phone, email, convertible, color, manufacturerCountry, importDuty)); } else if (line.equals("bicycle")) { numberSpeeds = Integer.parseInt(br.readLine()); vehicles.add(new Bicycle(ownerName, address, phone, email, numberSpeeds)); } else if (line.equals("truck")) { numberTons = Float.parseFloat(br.readLine()); truckCost = Float.parseFloat(br.readLine()); datePurchased = br.readLine(); vehicles.add(new Truck(ownerName, address, phone, email, numberTons, truckCost, datePurchased)); } line =br.readLine(); } } catch(Exception e) { System.out.println(e); return; } finally { br.close(); in.close(); } // convert to an array Vehicle[] va = new Vehicle[vehicles.size()]; va = vehicles.toArray(va); br = new BufferedReader(new InputStreamReader(System.in)); while (true) { System.out.println("\n\n Menu"); System.out.println(" ----\n\n"); System.out.println("1. Print all records.\n"); System.out.println("2. Sort records by email address.\n"); System.out.println("3. Print number of records.\n"); System.out.println("4. Print bicycle and truck records.\n"); System.out.println("5. Print vehicles in area code 904.\n\n"); int choice = 0; try { do { System.out.print("Your choice 1-5 or stop to close: "); String input = br.readLine(); if (input.toLowerCase().equals("stop")) return; choice = Integer.parseInt(input); } while(choice < 1 || choice > 5); } catch(Exception e) { System.out.println(e); } System.out.println(); switch(choice) { case 1: printAll(va); break; case 2: sort(va, true); break; case 3: printNumberRecords(va); break; case 4: printBicyclesAndTrucks(va); break; case 5: printAreaCode904(va); break; } } } private static void printAll(Vehicle[] va) { for(int i = 0; i < va.length; i++) { System.out.println("Vehicle type : " + va[i].getClass().getName()); System.out.println(va[i].toString()); System.out.println(); } } private static void sort(Vehicle[] va, boolean print) { Vehicle temp; for(int i = 0;i < va.length - 1;i++) { for(int j = i + 1; j < va.length;j++) { if(va[j].getEmail().compareTo(va[i].getEmail()) < 0) { temp = va[j]; va[j] = va[i]; va[i] = temp; } } } if (print) printAll(va); } private static void printNumberRecords(Vehicle[] va) { System.out.println("The number of records is " + va.length); System.out.println(); } private static void printBicyclesAndTrucks(Vehicle[] va) { sort(va, false); // make sure sorted first for(int i = 0; i < va.length; i++) { String typeName = va[i].getClass().getName(); if (typeName.equals("Bicycle") || typeName.equals("Truck")) { System.out.println("Vehicle type : " + typeName); System.out.println(va[i].toString()); System.out.println(); } } } private static void printAreaCode904(Vehicle[] va) { for(int i = 0; i < va.length; i++) { if (va[i].getPhone().equals("904") || typeName.equals("904")) { System.out.println("Vehicle type : " + va[i].getClass().getName()); System.out.println(va[i].toString()); System.out.println(); } } } }