【问题标题】:Switch Statement - charAt [closed]Switch 语句 - charAt [关闭]
【发布时间】:2023-07-17 19:06:02
【问题描述】:

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);

}

【问题讨论】:

  • 问题是……?
  • 请发布错误信息 -- 复制粘贴。并确保包括相关的线路标识。
  • 你确定 Ach 字符串不为空吗?
  • 代码的第一部分(printvehicleMAKE 方法之前)不在方法内。

标签: java string debugging switch-statement


【解决方案1】:

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

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

ch = Ach.length() > 0 ? Ach.charAt(0) : '\n';

【讨论】:

  • 他的代码好像不在方法中。
  • @assylias - 看起来像那样,但我怀疑 OP 只是忽略了这段代码的上下文(或者帖子有错字)。如果它不在方法中,则代码将无法编译,并且 OP 也无法使用调试器。
  • 除非他得到一个编译错误——并称之为调试错误?只是猜测...
  • @assylias - 也许是这样,但它会是一个奇怪的编译器,它会生成错误但不知道行号。另外,如果问题是没有封闭方法,为什么编译器会抱怨代码中的一行太远?