简体   繁体   English

indexOf()字符串类Java

[英]indexOf() String Class Java

I need help with comparing an indexOf from a string with an array of strings and display the correct matches. 我需要将字符串的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);   
}

Example. 例。 If I enter "toyota" or "aston", I want it to compare the 1st/2nd character of the string and return the matches from the array of strings. 如果输入“ toyota”或“ aston”,我希望它比较字符串的第1/2个字符并从字符串数组返回匹配项。

Assuming that 假如说

services[i].getvehicleMAKEMODEL())

gives the full string as in "Toyota Camry" you must adjust your filter criteria. 给出完整的字符串,如“ Toyota Camry”中所示,则必须调整过滤条件。 Currently you compare "to" with "Toyota Camry" . 当前,您将"to""Toyota Camry" This does not match. 这不匹配。 So you should use this instead 所以你应该改用这个

model.equalsIgnoreCase(services[i].getvehicleMAKEMODEL().substring(0,3))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM