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; String vin; public Car(String manufacturer, String model, int year, String vin) { 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; } } 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"); Car car = new Car(arr[0], arr[1], Integer.parseInt(arr[2]), arr[3]); list.add(car); } 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.vin.compareTo(car2.vin); } 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) { if (i == list.size() - 2 && list.get(i).manufacturer.equals(list.get(i + 1).manufacturer)) { oldest = (oldest.year < list.get(i + 1).year) ? oldest : list.get(i + 1); } count++; System.out.println(String.format("%15s%25s%5s %s", oldest.manufacturer, oldest.model, oldest.year, oldest.vin)); oldest = null; } } System.out.println(count + " result(s)"); } } i keep gettig the same issue and have tried everything, this error occurs everytime i run my program  Enter filename\n car-list-3.txtENTER Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1\n \tat Demo.main(Demo.java:52)\n it is occuring somewhere here while (scanner.hasNextLine()) { String[] arr = scanner.nextLine().split("\t"); Car car = new Car(arr[0], arr[1], Integer.parseInt(arr[2]), arr[3]); list.add(car); } can an expert help me resolve this issue by fixing this part of my coding, i have tried switching the line split \t to \\s+ and making my array 4 or 5, i need another solution

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

 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;
String vin;
public Car(String manufacturer, String model, int year, String vin) {
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;
}
}
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");
Car car = new Car(arr[0], arr[1], Integer.parseInt(arr[2]), arr[3]);
list.add(car);
}
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.vin.compareTo(car2.vin);
}
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) {
if (i == list.size() - 2 && list.get(i).manufacturer.equals(list.get(i + 1).manufacturer)) {
oldest = (oldest.year < list.get(i + 1).year) ? oldest : list.get(i + 1);
}
count++;
System.out.println(String.format("%15s%25s%5s %s", oldest.manufacturer, oldest.model, oldest.year, oldest.vin));
oldest = null;
}
}
System.out.println(count + " result(s)");
}
}

i keep gettig the same issue and have tried everything, this error occurs everytime i run my program 

Enter filename\n
car-list-3.txtENTER
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1\n
\tat Demo.main(Demo.java:52)\n

it is occuring somewhere here

while (scanner.hasNextLine()) {
String[] arr = scanner.nextLine().split("\t");
Car car = new Car(arr[0], arr[1], Integer.parseInt(arr[2]), arr[3]);
list.add(car);
}

can an expert help me resolve this issue by fixing this part of my coding, i have tried switching the line split \t to \\s+ and making my array 4 or 5, i need another solution

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Array
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