我需要帮助将字符串中的indexOf
与字符串数组进行比较,并显示正确的匹配项。
public static void printvehicleMAKE(ServiceAppointment[] services, Scanner input){
System.out.print("Enter vehicle make: ");
String make = input.next();
String model = make.substring(0,3);
System.out.println(model);
boolean flag = false;
System.out.println("Details of all vehicles manufactured by " +make+" : ");
for (int i=0;i<6;i++) {
if(model.equalsIgnoreCase(services[i].getvehicleMAKEMODEL())){
System.out.printf("\n%10s%15s%20s", services[i].getregoNUMBER(),
services[i].getbuildYEAR(), services[i].getvehicleMAKEMODEL());
flag = true;
}
}
if(flag==false)
System.out.print("No service appointments were found for vehicles "
+ "made by " + make);
}
实施例。如果我输入“toyota”或“aston”,我希望它比较字符串的第一个/第二个字符并返回字符串数组中的匹配。
答案 0 :(得分:0)
假设
services[i].getvehicleMAKEMODEL())
给出“丰田凯美瑞”中的完整字符串,您必须调整过滤条件。目前,您将"to"
与"Toyota Camry"
进行比较。这不匹配。所以你应该使用它来代替
model.equalsIgnoreCase(services[i].getvehicleMAKEMODEL().substring(0,3))