我在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);
}
答案 0 :(得分:0)
如果问题是为什么行号不可用,那么这可能是您如何打包执行代码的函数。听起来调试信息已被删除。
如果问题是您的代码在该行可能出错,答案是Ach
可能没有长度(如果输入是空行)。因此Ach.charAt(0)
将失败,因为没有第一个字符。你需要测试那个条件。
ch = Ach.length() > 0 ? Ach.charAt(0) : '\n';