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 with a specific line of my code here is my code import java.util.Comparator; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; 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 { @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 OldestCarsByMake { public static void main(String[] args) throws FileNotFoundException{ ArrayList cars = new ArrayList(); 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+"); 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)"); } } however this line of my code produces an error String model = carDetails[1]; i.e. Enter filename\n car-list-3.txtENTER Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1\n \tat OldestCarsByMake.main(OldestCarsByMake.java:68)\n Can an expert help fix this error to produce the correct outcome
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 with a specific line of my code
here is my code
import java.util.Comparator;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
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 OldestCarsByMake {
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+");
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)");
}
}
however this line of my code produces an error
String model = carDetails[1];
i.e.
Enter filename\n
car-list-3.txtENTER
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1\n
\tat OldestCarsByMake.main(OldestCarsByMake.java:68)\n
Can an expert help fix this error to produce the correct outcome
Trending now
This is a popular solution!
Step by step
Solved in 5 steps with 3 images