Java读取显示器数据文件出现Scanner输入不匹配错误怎么解决
程序Scanner相关InputMismatchException错误排查
问题说明
现有程序读取显示器参数数据文件时抛出java.util.Scanner相关的InputMismatchException异常,错误栈指向com.company.MonitorsTest.loadMonitors方法,需要定位错误原因并确认数据文件配置是否正确。
数据文件存储字段包含:显示器品牌、型号、刷新率、响应时间。
相关代码
class Monitors{ public String brandName; public int modelNumber; public int refreshRate; public int responseTime; } public class MonitorsTest { public static void main(String[] args) { Monitors[] gaming = new Monitors[100]; int dataFile; int choice; System.out.println("\nDisplay Monitors\n"); System.out.println(" Enter one of the following commands:"); System.out.println("(1)- Display the entire data file "); System.out.println("(2) - Display certain information on a monitor"); System.out.println("(3) - Display a histogram"); Scanner keyboard = new Scanner(System.in); System.out.println(); System.out.println("Enter 1, 2, or 3 "); choice = keyboard.nextInt(); dataFile = (loadMonitors(gaming)); while ( choice != 3){ if (choice < 1 || choice > 3) { System.out.println("Enter 1, 2, 3 "); choice = keyboard.nextInt(); } else if (choice == 1){ System.out.println(dataFile); } } } private static int loadMonitors(Monitors[] gaming) { int nMonitors = 0; try { File file = new File("C:\\Users\\kento\\IdeaProjects\\Program3\\src\\monitors.txt"); Scanner scan = new Scanner(file); do { gaming[nMonitors] = new Monitors(); gaming[nMonitors].brandName = scan.next(); gaming[nMonitors].modelNumber = scan.next(); gaming[nMonitors].responseTime = scan.nextDouble(); gaming[nMonitors].refreshRate = scan.nextInt(); ++nMonitors; } while (gaming[nMonitors - 1].refreshRate != 0); --nMonitors; } catch (IOException ioe){ System.out.println(" File access error" + ioe); nMonitors = 0; } return nMonitors; } }
错误日志
Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:864) at java.util.Scanner.next(Scanner.java:1485) at java.util.Scanner.nextInt(Scanner.java:2117) at java.util.Scanner.nextInt(Scanner.java:2076) at com.company.MonitorsTest.loadMonitors(MonitorsTest.java:62) at com.company.MonitorsTest.main(MonitorsTest.java:39)
数据文件内容
Sceptre DCIP3 1 165 AOC C24G1A 1 165 ASUS VG278QR 05 144 Sceptre E22 5 75 Alienware AW2521HF 1 240
错误根因
- 字段类型不匹配:
Monitors类中modelNumber定义为int类型,但实际数据文件中的型号值(如DCIP3、C24G1A)包含字母,无法转换为整数,直接触发类型转换异常。 - 读取方法和类型不对应:代码中给
modelNumber赋值时调用的是scan.next(),返回的是String类型,直接赋值给int变量本身就会报错;同时responseTime定义为int类型,但代码调用了scan.nextDouble()读取,类型不匹配。 - 循环终止条件错误:现有循环终止的判断条件是读取到的
refreshRate等于0,但提供的数据文件中没有refreshRate为0的行,读完所有文件内容后再调用读取方法会直接抛出异常。
修复方案
- 调整
Monitors类的字段类型,将modelNumber改为String类型适配带字母的型号数据。 - 修正读取逻辑的类型和顺序,适配数据文件的字段规则:
// 数据每行的字段顺序为:品牌、型号、响应时间、刷新率,全部对应类型读取 gaming[nMonitors].brandName = scan.next(); gaming[nMonitors].modelNumber = scan.next(); gaming[nMonitors].responseTime = scan.nextInt(); gaming[nMonitors].refreshRate = scan.nextInt();
- 调整循环读取逻辑,优先判断是否还有未读取的内容,避免读完文件后继续读取:
// 替换原有的do-while循环 while (scan.hasNext()) { gaming[nMonitors] = new Monitors(); gaming[nMonitors].brandName = scan.next(); gaming[nMonitors].modelNumber = scan.next(); gaming[nMonitors].responseTime = scan.nextInt(); gaming[nMonitors].refreshRate = scan.nextInt(); ++nMonitors; // 如果要保留原有refreshRate=0终止的逻辑,可在这里加判断 if (gaming[nMonitors - 1].refreshRate == 0) { --nMonitors; break; } }
- 读取完成后调用
scan.close()释放Scanner资源。 - 数据文件无需调整,现有格式符合空白符分割的读取规则。
内容的提问来源于stack exchange,提问作者SimplyKenton
相关产品推荐
相关产品推荐

