Ask the user for a filename. Display the oldest car for every manufacturer from that file. If two cars have the same year, compare based on the VIN. i am having trouble getting my code to work I have this code: import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; class Car { String manufacturer; String model; int year; String vin; public Car(String manufacturer, String model, int year, String vin) { this.manufacturer = manufacturer; this.model = model; this.year = year; this.vin = vin; } public String getManufacturer() { return manufacturer; } public void setManufacturer(String manufacturer) { this.manufacturer = manufacturer; } public String getModel() { return model; } public void setModel(String model) { this.model = model; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public String getVin() { return vin; } public void setVin(String vin) { this.vin = vin; } } public class Demo { public static void main(String[] args) throws FileNotFoundException { Scanner keyboard = new Scanner(System.in); System.out.println("Enter filename"); String input = keyboard.nextLine(); File file = new File(input); Scanner scanner = new Scanner(file); scanner.nextLine(); ArrayList list = new ArrayList<>(); while (scanner.hasNextLine()) { String[] arr = scanner.nextLine().split(" "); if (arr.length >= 4) { Car car = new Car(arr[0], arr[1], Integer.parseInt(arr[2]), arr[3]); list.add(car); } } for (int end = list.size() - 1; end >= 1; end--) { for (int current = 0; current <= end - 1; current++) { Car car1 = list.get(current); Car car2 = list.get(current + 1); int n = car1.manufacturer.toLowerCase().compareTo(car2.manufacturer.toLowerCase()); if (n == 0) { n = car1.year - car2.year; } if (n == 0) { n = car1.vin.compareTo(car2.vin); } if (n > 0) { Car temp = list.get(current); list.set(current, list.get(current + 1)); list.set(current + 1, temp); } } } // Find the oldest car for each manufacturer and print the results System.out.println("Oldest cars by make"); Car oldest = null; int count = 0; for (int i = 0; i < list.size() - 1; i++) { if (oldest == null) { oldest = list.get(i); } if (!list.get(i).manufacturer.equals(list.get(i + 1).manufacturer) || i == list.size() - 2) { if (i == list.size() - 2 && list.get(i).manufacturer.equals(list.get(i + 1).manufacturer)) { oldest = (oldest.year < list.get(i + 1).year) ? oldest : list.get(i + 1); } count++; System.out.println(String.format("%15s%25s%5s %s", oldest.manufacturer, oldest.model, oldest.year, oldest.vin)); oldest = null; } } System.out.println(count + " result(s)"); } } it is supposed to display Enter filename\n car-list-3.txtENTER Oldest cars by make\n Acura Legend 1988 YV1672MK8A2784103\n Aptera Typ-1 2009 19UUA56952A698282\n Aston Martin DB9 2006 JTDJTUD36ED662608\n Audi 5000S 1985 JN1CV6EK1CM209730\n Austin Mini Cooper 1964 1G6KD57Y86U095255\n Bentley Continental GT 2006 19UUA655X4A245718\n BMW 7 Series 1993 1FM5K7B80FG198521\n Buick LeSabre 1986 5FRYD4H82EB970786\n Cadillac Fleetwood 1992 WAUSVAFA0AN244068\n Chevrolet Corvette 1957 WBA3V7C57F5095471\n Chrysler Town & Country 1994 WBANU53558B610150\n Daewoo Lanos 1999 2HNYD18412H169358\n Daihatsu Charade 1992 JH4CL95958C112754\n... 56 result(s)\n but it is diaplying errors Enter filename\n car-list-3.txtENTER Exception in thread "main" java.lang.NumberFormatException: For input string: "Rover"\n \tat java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)\n \tat java.base/java.lang.Integer.parseInt(Integer.java:665)\n \tat java.base/java.lang.Integer.parseInt(Integer.java:781)\n \tat Demo.main(Demo.java:53)\n i need my code to be fixed so that is displays the information properly to display the oldest car for every manufacturer from that file and If two cars have the same year, compare based on the VIN. i am not sure where i am going wrong with my code, the code is based on sorting and searching
Ask the user for a filename. Display the oldest car for every manufacturer from that file. If two cars have the same year, compare based on the VIN.
i am having trouble getting my code to work
I have this code:
import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; class Car { String manufacturer; String model; int year; String vin; public Car(String manufacturer, String model, int year, String vin) { this.manufacturer = manufacturer; this.model = model; this.year = year; this.vin = vin; } public String getManufacturer() { return manufacturer; } public void setManufacturer(String manufacturer) { this.manufacturer = manufacturer; } public String getModel() { return model; } public void setModel(String model) { this.model = model; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public String getVin() { return vin; } public void setVin(String vin) { this.vin = vin; } } public class Demo { public static void main(String[] args) throws FileNotFoundException { Scanner keyboard = new Scanner(System.in); System.out.println("Enter filename"); String input = keyboard.nextLine(); File file = new File(input); Scanner scanner = new Scanner(file); scanner.nextLine(); ArrayList<Car> list = new ArrayList<>(); while (scanner.hasNextLine()) { String[] arr = scanner.nextLine().split(" "); if (arr.length >= 4) { Car car = new Car(arr[0], arr[1], Integer.parseInt(arr[2]), arr[3]); list.add(car); } } for (int end = list.size() - 1; end >= 1; end--) { for (int current = 0; current <= end - 1; current++) { Car car1 = list.get(current); Car car2 = list.get(current + 1); int n = car1.manufacturer.toLowerCase().compareTo(car2.manufacturer.toLowerCase()); if (n == 0) { n = car1.year - car2.year; } if (n == 0) { n = car1.vin.compareTo(car2.vin); } if (n > 0) { Car temp = list.get(current); list.set(current, list.get(current + 1)); list.set(current + 1, temp); } } } // Find the oldest car for each manufacturer and print the results System.out.println("Oldest cars by make"); Car oldest = null; int count = 0; for (int i = 0; i < list.size() - 1; i++) { if (oldest == null) { oldest = list.get(i); } if (!list.get(i).manufacturer.equals(list.get(i + 1).manufacturer) || i == list.size() - 2) { if (i == list.size() - 2 && list.get(i).manufacturer.equals(list.get(i + 1).manufacturer)) { oldest = (oldest.year < list.get(i + 1).year) ? oldest : list.get(i + 1); } count++; System.out.println(String.format("%15s%25s%5s %s", oldest.manufacturer, oldest.model, oldest.year, oldest.vin)); oldest = null; } } System.out.println(count + " result(s)"); } }
it is supposed to display
Enter filename\n
car-list-3.txtENTER
Oldest cars by make\n
Acura Legend 1988 YV1672MK8A2784103\n
Aptera Typ-1 2009 19UUA56952A698282\n
Aston Martin DB9 2006 JTDJTUD36ED662608\n
Audi 5000S 1985 JN1CV6EK1CM209730\n
Austin Mini Cooper 1964 1G6KD57Y86U095255\n
Bentley Continental GT 2006 19UUA655X4A245718\n
BMW 7 Series 1993 1FM5K7B80FG198521\n
Buick LeSabre 1986 5FRYD4H82EB970786\n
Cadillac Fleetwood 1992 WAUSVAFA0AN244068\n
Chevrolet Corvette 1957 WBA3V7C57F5095471\n
Chrysler Town & Country 1994 WBANU53558B610150\n
Daewoo Lanos 1999 2HNYD18412H169358\n
Daihatsu Charade 1992 JH4CL95958C112754\n...
56 result(s)\n
but it is diaplying errors
Enter filename\n
car-list-3.txtENTER
Exception in thread "main" java.lang.NumberFormatException: For input string: "Rover"\n
\tat java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)\n
\tat java.base/java.lang.Integer.parseInt(Integer.java:665)\n
\tat java.base/java.lang.Integer.parseInt(Integer.java:781)\n
\tat Demo.main(Demo.java:53)\n
i need my code to be fixed so that is displays the information properly to display the oldest car for every manufacturer from that file and If two cars have the same year, compare based on the VIN.
i am not sure where i am going wrong with my code, the code is based on sorting and searching
Trending now
This is a popular solution!
Step by step
Solved in 5 steps with 3 images