-1

我在 String.charAt(int) 行出现调试错误:该行不可用
ch = Ach.charAt(0);

char ch;
String Ach;    
Scanner input = new Scanner(System.in);
printvehicleMAKE(services, input);

    do{
        System.out.println("\n    Service Item Recording Feature");
        System.out.println("      ------------------------------");
        System.out.println("\n\n A - Record Part Details");
        System.out.println("\n B - Add Labour Hours");
        System.out.println("\n X - Exit service item recording feature");
        System.out.print("\n\nEnter your selection: ");
        Ach = input.nextLine().toUpperCase();
        ch = Ach.charAt(0);
        switch(ch){
            case 'A':
                System.out.print("Enter registration number of vehicle: ");
                String inputREGO = input.nextLine();
                boolean flag = false;
                for(int i=0; i<6; i++){
                    if(inputREGO.equalsIgnoreCase(services[i].getregoNUMBER())){
                        System.out.print("Enter Part Description: ");
                        String parts = input.nextLine();
                        System.out.print("Enter Part Cost: ");
                        Double cost = input.nextDouble();
                        services[i].addPARTDETAILS(parts, cost);
                        flag = true;
                    }
                }
                if(!flag)
                    System.out.println("No registration number were found in the system.");

                break; 
            case 'B' :
                break;
            case 'X' :
                System.out.println("Exiting system......");
                break;
            default: System.out.println("Error - invalid selection entered!");
            break;
        }
    }while(ch!='X');

public static void printvehicleMAKE(ServiceAppointment[] services, Scanner
        input){
    System.out.print("Enter vehicle make: ");
    String make = input.nextLine();
    boolean flag = false;
    for (int i=0;i<6;i++){
        if (services[i].getvehicleMAKEMODEL().indexOf(make) != -1){
            System.out.printf("\n%-10s%-8s%10s", services[i].getregoNUMBER(),
                    services[i].getbuildYEAR(), services[i].getvehicleMAKEMODEL());
                    flag=true;
        }
    }if(!flag)
        System.out.println("No service appointments were found for vehicles " 
                + "made by " + make);

}
4

1 回答 1

0

如果问题是为什么行号不可用,那可能是您如何打包代码以供执行的功能。听起来调试信息已被剥离。

如果问题是您的代码在该行可能有什么问题,则答案是Ach长度可能为零(如果输入是空行)。因此Ach.charAt(0)将失败,因为没有第一个字符。您需要测试这种情况。

ch = Ach.length() > 0 ? Ach.charAt(0) : '\n';
于 2013-04-08T16:04:15.480 回答