year, compare based on the VIN. i am having trouble getting my code to work this is my code: import java.util.*; import java.io.*; class Car { String manufacturer; String model; int year; String vin; public Car(String
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
this is my code:
import java.util.*;
import java.io.*;
class Car {
String manufacturer;
String model;
int year;
String vin;
public Car(String manufacturer, String model, int year, String vin) {
super();
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;
}
}
class CarComparator implements Comparator < Car > {
@Override
public int compare(Car car1, Car car2) {
int yearCompare = Integer.compare(car1.getYear(), car2.getYear());
if (yearCompare != 0) {
return yearCompare;
}
return car1.getVin().compareTo(car2.getVin());
}
}
public class Demo {
public static void main(String[] args) throws FileNotFoundException {
ArrayList < Car > cars = new ArrayList < Car > ();
Scanner sc1 = new Scanner(System.in);
System.out.println("Enter filename");
String carFile = sc1.next();
File file = new File(carFile);
Scanner sc2 = new Scanner(file);
String[] carDetails;
sc2.nextLine();
while (sc2.hasNext()) {
String carLine = sc2.nextLine();
carDetails = carLine.split("\\s+");
if (carDetails.length < 2) {
continue;
}
String manufacturer = carDetails[0];
String model = carDetails[1];
int year = Integer.parseInt(carDetails[2]);
String vin = carDetails[3];
boolean flag = true;
for (Car c : cars) {
if (c.manufacturer.equals(manufacturer)) {
flag = false;
if (c.year > year) {
c.setModel(model);
c.setYear(year);
c.setVin(vin);
} else if (c.year == year) {
if ((c.vin).compareTo(vin) > 0) {
c.setModel(model);
c.setYear(year);
c.setVin(vin);
}
}
}
}
if (flag) {
cars.add(new Car(manufacturer, model, year, vin));
}
}
Collections.sort(cars, new CarComparator());
int spaces = 25;
for (Car c: cars) {
for (int i = 0; i < 20 - (c.manufacturer.length()); i++)
System.out.print(" ");
System.out.print(c.manufacturer);
for (int i = 0; i < 20 - (c.model.length()); i++)
System.out.print(" ");
System.out.println(c.model + " " + c.year + " " + c.vin);
}
System.out.println(cars.size() + " 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 displaying the same error everytime
car-list-3.txtENTER
Exception in thread "main" java.lang.NumberFormatException: For input string: "2500,2004,1GKS1EEF3DR855619"\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:68)\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 and
Trending now
This is a popular solution!
Step by step
Solved in 6 steps with 2 images