I need help with my code 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. here is my code so far import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; class Car { String manufacturer; String model; int year; public Car(String manufacturer, String model, int year) { super(); this.manufacturer = manufacturer; this.model = model; this.year = year; } 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 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("\t"); } 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.model.toLowerCase().compareTo(car2.model.toLowerCase()); } if (n > 0) { Car temp = list.get(current); list.set(current, list.get(current + 1)); list.set(current + 1, temp); } } } 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) { count++; System.out.println(String.format("%15s%25s%5s", oldest.manufacturer, oldest.model, oldest.year)); oldest = null; } } System.out.println(count + " result(s)"); } } i still need to implement the make, model, year, and vin number but i am getting lost on how to read it from any given file when i enter my code i get: Enter filename\n car-list-3.txtENTER Oldest cars by make\n 0 result(s)\n it needs to be : 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... 56 result(s)\n can an expert fix my code and explain what is going wrong with my code
I need help with my code
here is my code so far
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
class Car {
String manufacturer;
String model;
int year;
public Car(String manufacturer, String model, int year) {
super();
this.manufacturer = manufacturer;
this.model = model;
this.year = year;
}
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 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("\t");
}
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.model.toLowerCase().compareTo(car2.model.toLowerCase());
}
if (n > 0)
{
Car temp = list.get(current);
list.set(current, list.get(current + 1));
list.set(current + 1, temp);
}
}
}
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)
{
count++;
System.out.println(String.format("%15s%25s%5s", oldest.manufacturer, oldest.model, oldest.year));
oldest = null;
}
}
System.out.println(count + " result(s)");
}
}
i still need to implement the make, model, year, and vin number but i am getting lost on how to read it from any given file
when i enter my code i get:
Enter filename\n
car-list-3.txtENTER
Oldest cars by make\n
0 result(s)\n
it needs to be :
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...
56 result(s)\n
can an expert fix my code and explain what is going wrong with my code
Step by step
Solved in 4 steps with 1 images