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.

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 have this code so far

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) { return car1.getManufacturer().compareTo(car2.getManufacturer()); } } 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 = new String[4]; sc2.nextLine(); //Skipping header while (sc2.hasNext()) { String carLine = sc2.nextLine(); carDetails = carLine.split("\\s\\s\\s"); String manufacturer = carDetails[1]; String model = carDetails[2]; int year = Integer.parseInt(carDetails[3]); String vin = carDetails[4]; 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.print(c.model); System.out.println(" " + c.year + " " + c.vin); } System.out.println(cars.size() + " result(s)"); } }

however it produces an error

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:69)\n

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
Dodge D150 1993 19XFA1F37BE391950\n
Eagle Talon 1993 WUAAU34289N943973\n
Ferrari F430 Spider 2006 5N1AR2MM5EC533806\n
Fiat 500 2012 1G6DL67A880617705\n
Ford Thunderbird 1965 1HGCR2F32DA757676\n
Geo Metro 1992 1G6KE54Y34U777569\n
GMC Suburban 1500 1992 19XFB2F59CE612826\n
Holden VS Commodore 1995 WAUBGBFC7CN714505\n
Honda Civic 1980 JTDKN3DU8A0870976\n
Hummer H1 1999 5FNRL5H20BB474763\n
Hyundai Sonata 2002 JA4AS2AW2DU975492\n
Infiniti Q 1994 WAUDH78E97A027816\n
Isuzu Impulse 1992 WBALM5C5XAE656665\n
Jaguar XJ Series 1994 1FMJK1HT9FE090336\n
Jeep Wrangler 1992 WA1AY94L38D656200\n
Kia Sephia 2001 3GYFNCEY3BS991891\n
Lamborghini Countach 1986 SCBGU3ZA2EC356135\n ............

56 result(s)\n

can an expert help explain what's causing this error and fix my code to produce the correct output that is pulled from a user entered .txt file

 

Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Multiple table
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
  • SEE MORE 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