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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 5 steps with 3 images

Blurred answer
Knowledge Booster
Files and Directory
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education