如何从静态方法向对象数组传值?程序输入报错求助
嘿,我来帮你搞定这几个Java代码的问题!咱们一步步拆解,先解决输入错误,再完成未实现的功能:
问题解决指南:输入修复与功能实现
1. 修复Scanner首次输入异常
你的scannerUserInput类每次调用方法都新建Scanner实例,而且getString()用nextLine()时,前面的nextInt()会留下换行符,导致首次读取空字符串。咱们改成全局单例Scanner,同时处理输入后的换行缓冲区:
package eymain; import java.util.Scanner; public class scannerUserInput { // 全局唯一Scanner实例,避免重复创建和换行问题 private static final Scanner ob = new Scanner(System.in); static int getInteger(){ try{ int i = ob.nextInt(); ob.nextLine(); // 读取掉nextInt后的换行符 return i; } catch(Exception e){ ob.nextLine(); // 出错时清空缓冲区,避免后续输入异常 return -1; } } static double getDouble(){ try{ double d = ob.nextDouble(); ob.nextLine(); return d; } catch(Exception e){ ob.nextLine(); return -1; } } static String getString(){ try{ return ob.nextLine().trim(); // 去掉前后空格,避免无效输入 } catch(Exception e){ return ""; } } // 其他方法(getByte、getShort等)同理添加换行处理,这里省略重复代码 static byte getByte(){ try{ byte b = ob.nextByte(); ob.nextLine(); return b; } catch(Exception e){ ob.nextLine(); return -1; } } static short getShort(){ try{ short s = ob.nextShort(); ob.nextLine(); return s; } catch(Exception e){ ob.nextLine(); return -1; } } static long getLongInteger(){ try{ long l = ob.nextLong(); ob.nextLine(); return l; } catch(Exception e){ ob.nextLine(); return -1; } } static float getFloat(){ try{ float f = ob.nextFloat(); ob.nextLine(); return f; } catch(Exception e){ ob.nextLine(); return -1; } } static char getChar(){ try{ char ch = ob.next().charAt(0); ob.nextLine(); return ch; } catch(Exception e){ ob.nextLine(); return ' '; } } }
2. 实现writeUSB的内存过滤功能
原来的writeUSB返回类型错误(应该是int索引而非double),且没有过滤逻辑。咱们修改为只保留memorySpace ≤8GB的元素,并返回最后一个有效元素的索引:
package eymain; public class eYMethods { // 返回最后一个有效元素的索引,过滤内存超过8GB的元素 static int writeUSB(ekpaideytikoYliko usb[]) { int lastValidIndex = -1; // 初始无有效元素 for(int i = 0; i < usb.length; i++) { System.out.println("Give fileName : "); String fileName = scannerUserInput.getString(); System.out.println("Give minutes : "); double minutes = scannerUserInput.getDouble(); System.out.println("Give memorySpace (GB): "); double memorySpace = scannerUserInput.getDouble(); // 只保留符合内存要求的元素 if (memorySpace <= 8) { usb[i] = new ekpaideytikoYliko(fileName, minutes, memorySpace); lastValidIndex = i; // 更新最后有效索引 } else { usb[i] = null; // 不符合条件的位置置空,方便后续判断 } } return lastValidIndex; } // 其他代码... }
注意:原来的代码直接调用setXXX会触发空指针异常,因为数组初始化后元素都是null,必须先创建对象再赋值。
3. 修复ekpaideytikoYliko的getfileType方法
原来的方法参数冗余(不需要数组),它是实例方法,只需处理当前对象的文件名:
package eymain; public class ekpaideytikoYliko { // 原有成员变量、构造方法、get/set方法不变,这里省略重复代码 private String fileName; private double minutes; private double memorySpace; ekpaideytikoYliko(String fileName, double minutes, double memorySpace) { this.fileName = fileName; this.minutes = minutes; this.memorySpace = memorySpace; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public double getMinutes() { return minutes; } public void setMinutes(double minutes) { this.minutes = minutes; } public double getMemorySpace() { return memorySpace; } public void setMemorySpace(double memorySpace) { this.memorySpace = memorySpace; } // 修复后的文件扩展名提取方法 public String getfileType() { int dotIndex = fileName.lastIndexOf("."); // 处理无扩展名或扩展名只有一个点的情况 if (dotIndex == -1 || dotIndex == fileName.length() - 1) { return ""; } return fileName.substring(dotIndex).toLowerCase(); // 转小写避免大小写差异 } }
4. 实现showDocs筛选打印.doc/.docx文件
修改方法,只打印符合扩展名要求的有效元素:
package eymain; public class eYMethods { // 原有writeUSB方法不变,这里省略... static void showDocs(ekpaideytikoYliko usb[]) { System.out.println("\n--- .doc/.docx 文件列表 ---"); for(int i = 0; i < usb.length; i++) { if (usb[i] == null) continue; // 跳过无效元素 String fileType = usb[i].getfileType(); // 判断是否为目标扩展名(不区分大小写) if (fileType.equals(".doc") || fileType.equals(".docx")) { System.out.println("fileName : " + usb[i].getFileName()); System.out.println("minutes : " + usb[i].getMinutes()); System.out.println("memorySpace : " + usb[i].getMemorySpace()); System.out.println("------------------------"); } } } }
5. 最后调整EyMain类
确保逻辑正确,新增打印最后有效索引的提示:
public class EyMain { public static void main(String[] args) { int n; do { System.out.println("Give an integer value > 100 : "); n = scannerUserInput.getInteger(); } while (n < 101); ekpaideytikoYliko usb[] = new ekpaideytikoYliko[n]; int lastValid = eYMethods.writeUSB(usb); System.out.println("最后一个有效元素的索引: " + lastValid); eYMethods.showDocs(usb); } }
这样所有问题就都解决了:Scanner输入异常修复,内存过滤功能实现,指定扩展名文件筛选打印完成,方法参数错误也修正了。
内容的提问来源于stack exchange,提问作者Lazos Pap
相关产品推荐
相关产品推荐

