This code asks user to enter vehicle object that has name, model year, listing price, and percent Discount. The problem that is occurring here, when user enters all of the above info the car object is added to the bottom of the array list and not in the alphabetical order. Note the list was alphabetized before.
while (!valid) {
String str = scan.nextLine();
try {
boolean found = false;
System.out.println("Enter car name: ");
name = scan.nextLine();
System.out.println("Enter car model year: ");
modelYear = scan.nextLine();
System.out.println("Enter car list price: ");
listPrice = scan.nextDouble();
System.out.println("Enter car percent discount: ");
percentDiscount = scan.nextDouble();
int i = 0;
loc = 0;
while (!found && i < carList.size()) {
String nameRetrievedFromCarList = carList.get(i).getName();
String nameToAdd = "";
if (nameToAdd.compareToIgnoreCase(nameRetrievedFromCarList) < 0) {
loc++;
found = true;
}
i++;
}// end while
Proj1CarData newCar = new Proj1CarData(name, modelYear, listPrice, percentDiscount,
discountAmount, netPrice);
carList.add(loc, newCar);
valid = true;
}// end try
catch (NumberFormatException nfe) {
System.out.println("Wrong entry: Try again");
}// end catch
}