I am getting an error when running my main method and I cannot figure out what I have wrong. The error I'm seeing is:
Exception in thread "main" java.lang.ClassCastException: Car cannot be cast to java.base/java.lang.Comparable.
The error is occurring when calling on the listByOwner
method, Arrays.sort(vehicleList)
. I do not believe I have two vehicleList arrays, as I know that could possibly cause an issue sorting my vehicleList array.
My code:
import java.util.Arrays;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.text.DecimalFormat;
public class UseTaxList {
private String taxDistrict;
private Vehicle[] vehicleList;
private String[] excludedRecords;
public UseTaxList() {
taxDistrict = "not yet assigned";
vehicleList = new Vehicle[0];
excludedRecords = new String[0];
}
public void readVehicleFile(String fileNameIn)
throws FileNotFoundException {
Scanner scanner = new Scanner(new File(fileNameIn));
taxDistrict = scanner.nextLine().trim();
while (scanner.hasNext()) {
String s = scanner.nextLine().trim();
Scanner lineScanner = new Scanner(s);
lineScanner.useDelimiter(";");
char vehicleType = lineScanner.next().toUpperCase().trim().charAt(0);
String owner = lineScanner.next().trim();
String yearMakeModel = lineScanner.next().trim();
String v = lineScanner.next().trim();
String aF = lineScanner.next().trim();
switch (vehicleType) {
case 'C':
double carValue = Double.parseDouble(v);
boolean carAltFuel = Boolean.parseBoolean(aF);
Car newCar = new Car(owner, yearMakeModel, carValue, carAltFuel);
addVehicle(newCar);
break;
case 'T':
double truckValue = Double.parseDouble(v);
boolean truckAltFuel = Boolean.parseBoolean(aF);
String t = lineScanner.next().trim();
double tons = Double.parseDouble(t);
Truck newTruck = new Truck(owner, yearMakeModel, truckValue,
truckAltFuel, tons);
addVehicle(newTruck);
break;
case 'S':
double semiValue = Double.parseDouble(v);
boolean semiAltFuel = Boolean.parseBoolean(aF);
String st = lineScanner.next().trim();
double semiTons = Double.parseDouble(st);
String ax = lineScanner.next().trim();
int axle = Integer.parseInt(ax);
SemiTractorTrailer newSemi = new SemiTractorTrailer(owner,
yearMakeModel, semiValue, semiAltFuel, semiTons, axle);
addVehicle(newSemi);
break;
case 'M':
String e = lineScanner.next().trim();
double engSize = Double.parseDouble(e);
double mValue = Double.parseDouble(v);
boolean mAltFuel = Boolean.parseBoolean(aF);
Motorcycle newMotorcycle = new Motorcycle(owner,
yearMakeModel, mValue, mAltFuel, engSize);
addVehicle(newMotorcycle);
break;
default:
addExcludedRecords("Excluded Records:\n" + s);
break; }
}
}
public String getTaxDistrict() {
return taxDistrict;
}
public void setTaxDistrict(String taxDistrictIn) {
taxDistrict = taxDistrictIn;
}
public Vehicle[] getVehicleList() {
return vehicleList;
}
public String[] getExcludedRecords() {
return excludedRecords;
}
public void addVehicle(Vehicle vehicleIn) {
vehicleList = Arrays.copyOf(vehicleList, vehicleList.length + 1);
vehicleList[vehicleList.length - 1] = vehicleIn;
}
public void addExcludedRecords(String recordsIn) {
excludedRecords = Arrays.copyOf(excludedRecords,
excludedRecords.length + 1);
excludedRecords[excludedRecords.length - 1] = recordsIn;
}
public String toString() {
String output = "";
for (Vehicle v1 : vehicleList) {
output += "\n" + v1 + "\n"; }
return output;
}
public double calculateTotalUseTax() {
double total = 0.0;
for (Vehicle v1 : vehicleList) {
total += v1.useTax();
}
return total;
}
public double calculateTotalValue() {
double total = 0.0;
for (Vehicle v1 : vehicleList) {
total += v1.getValue();
}
return total;
}
public String summary() {
DecimalFormat formatter = new DecimalFormat("##,###.00");
String output = "";
output += "------------------------------\n";
output += "Summary for " + taxDistrict + "\n";
output += "------------------------------";
output += "\nNumber of Vehicles: " + vehicleList.length;
output += "\nTotal Value: $" + formatter.format(calculateTotalValue());
output += "\nTotal Use Tax: $" + formatter.format(calculateTotalUseTax())
+ "\n";
return output;
}
public String listByOwner() {
Arrays.sort(vehicleList);
String output = "------------------------------\n";
output += "Vehicles by Owner\n";
output += "------------------------------\n";
for (Vehicle v1 : vehicleList) {
output += "\n" + v1 + "\n";
}
return output;
}
public String listByUseTax() {
Arrays.sort(vehicleList, new UseTaxComparator());
String output = "------------------------------\n";
output += "Vehicles by Use Tax\n";
output += "------------------------------\n";
for (Vehicle v1 : vehicleList) {
output += "\n" + v1 + "\n";
}
return output;
}
public String excludedRecordsList() {
String output = "";
output += "------------------------------\n";
output += "Excluded Records\n";
output += "------------------------------\n";
for (String v1 : excludedRecords) {
output += "\n" + v1 + "\n";
}
return output;
}
}
According to the error you're getting, your Vehicle
class doesn't implement the Comparable
interface, so you'll have to explicitly pass a Comparator
to the sort
call:
Arrays.sort(vehicleList, Comparator.comparing(Vehicle::getOwner));