Java销售追踪器ID长度校验问题:修复int无法调用length()错误
Java销售追踪器ID长度校验错误修复方案
错误原因
你遇到的int cannot be dereferenced错误,是因为代码里的id是int基本数据类型,而length()是String类专属方法——基本数据类型没有可调用的方法,直接使用id.length()会触发编译错误。另外原代码中id在循环前未初始化,也会导致编译失败。
修复方案
由于ID要求是8位字符长度(可能包含前导零,比如00123456),推荐将ID当作字符串处理,这样能完整保留格式,避免数字类型丢失前导零的问题。
方案1:将ID作为字符串处理(推荐)
修改addOption方法中的相关代码:
public static int addOption(Salesperson[] array, int count) { Scanner input = new Scanner(System.in); String id; // 把int类型改为String double sales; String name; boolean alreadyEntered; if(count == array.length) System.out.println("Sorry - array is full -- cannot add a record"); else { System.out.print("Enter name of salesperson >> "); name = input.nextLine(); // 初始化id为null,进入循环校验 id = null; while(id == null || id.length() != 8) { System.out.print("Enter salesperson ID >> "); id = input.nextLine().trim(); // 额外校验是否为纯数字 if(!id.matches("\\d{8}")) { System.out.println("ID必须是8位数字,请重新输入"); id = null; // 重置id,让循环继续 } } alreadyEntered = false; // 对比ID时,将字符串转成int(适配Salesperson类的int类型id) int idNum = Integer.parseInt(id); for(int x = 0; x < count; ++x) if(array[x].getId() == idNum) { System.out.println("Sorry -- ID number already exists"); alreadyEntered = true; } if(!alreadyEntered) { System.out.print("Enter sales amount >> "); sales = input.nextDouble(); array[count] = new Salesperson(idNum, sales, name); ++count; } } display(array, count); input.nextLine(); return count; }
方案2:坚持用int类型校验位数(不推荐,丢失前导零)
如果不需要保留前导零,仅要求数字是8位,可以通过范围判断实现:
public static int addOption(Salesperson[] array, int count) { Scanner input = new Scanner(System.in); int id; double sales; String name; boolean alreadyEntered; if(count == array.length) System.out.println("Sorry - array is full -- cannot add a record"); else { System.out.print("Enter name of salesperson >> "); name = input.nextLine(); // 循环校验数字是否在8位范围内 do { System.out.print("Enter salesperson ID >> "); id = input.nextInt(); if(id < 10000000 || id > 99999999) { System.out.println("ID必须是8位数字,请重新输入"); } } while(id < 10000000 || id > 99999999); alreadyEntered = false; for(int x = 0; x < count; ++x) if(array[x].getId() == id) { System.out.println("Sorry -- ID number already exists"); alreadyEntered = true; } if(!alreadyEntered) { System.out.print("Enter sales amount >> "); sales = input.nextDouble(); array[count] = new Salesperson(id, sales, name); ++count; } } display(array, count); input.nextLine(); return count; }
扩展:修改ID时的校验
在changeOption方法中,同样需要添加ID长度校验,用字符串方案修改如下:
public static void changeOption(Salesperson[] array, int count) { Scanner keyboard = new Scanner(System.in); int position = 0; String idStr; // 改为String类型 double sales; if(count == 0) System.out.println("Database is empty -- cannot change record"); else { idStr = null; while(idStr == null || idStr.length() !=8) { System.out.print("Enter ID to change >> "); idStr = keyboard.nextLine().trim(); if(!idStr.matches("\\d{8}")) { System.out.println("ID必须是8位数字,请重新输入"); idStr = null; } } int id = Integer.parseInt(idStr); boolean exists = false; for(int x = 0; x < count; ++x) if(array[x].getId() == id) { exists = true; position = x; } if(!exists) System.out.println("Sorry - ID number #" + id + " does not exists in the database"); else { System.out.print("Enter sales >> "); sales = keyboard.nextDouble(); array[position].setSales(sales); } keyboard.nextLine(); display(array, count); } }
内容的提问来源于stack exchange,提问作者Cody Mangmoun
相关产品推荐
相关产品推荐

