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
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;
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
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
Trending now
This is a popular solution!
Step by step
Solved in 2 steps