0

我需要帮助将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”,我希望它比较字符串的第一个/第二个字符并从字符串数组中返回匹配项。

4

1 回答 1

0

假如说

services[i].getvehicleMAKEMODEL())

给出完整的字符串,如“Toyota Camry”,你必须调整你的过滤条件。目前您"to""Toyota Camry". 这不匹配。所以你应该改用这个

model.equalsIgnoreCase(services[i].getvehicleMAKEMODEL().substring(0,3))
于 2013-04-07T11:23:08.230 回答