java - 记录数组和文件处理
问题描述
我试图弄清楚为什么我的文件不会输出。它是一个数据文件,包括显示器及其刷新率、响应时间、显示器型号和品牌。每当我运行它时,我都会遇到 at.util.Scanner 错误和 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();// Error here
gaming[nMonitors].modelNumber = scan.next();// Error here
gaming[nMonitors].responseTime = scan.nextDouble();
gaming[nMonitors].refreshRate = scan.nextInt();// Error here
++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
解决方案
问题代码不断变化,因此无法 100% 指出确切的问题,但是,我怀疑数据与解析工作流程不匹配。
对我来说,我会读取下一行数据,然后单独解析它,例如:
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
System.out.println(loadMonitors(new Monitors[100]));
}
class Monitors {
public String brandName;
public String modelNumber;
public int refreshRate;
public double responseTime;
}
private int loadMonitors(Monitors[] gaming) {
int nMonitors = 0;
try (InputStream is = getClass().getResourceAsStream("/stackoverflow/monitors.txt"); Scanner scan = new Scanner(is)) {
while (scan.hasNextLine()) {
String line = scan.nextLine();
Scanner parser = new Scanner(line);
System.out.println(line);
Monitors monitor = new Monitors();
monitor.brandName = parser.next();
monitor.modelNumber = parser.next();
monitor.responseTime = parser.nextDouble();
monitor.refreshRate = parser.nextInt();
gaming[nMonitors] = monitor;
++nMonitors;
}
} catch (IOException ioe) {
System.out.println(" File access error" + ioe);
nMonitors = 0;
}
return nMonitors;
}
}
这将输出
Sceptre DCIP3 1 165
AOC C24G1A 1 165
ASUS VG278QR 0.5 144
Sceptre E22 5 75
Alienware AW2521HF 1 240
5
*nb:请注意,我将monitors.txt
文件视为嵌入式资源,因为您的原始代码已将其存储在src
目录中,所以上面的示例是更正确的读取方式。
推荐阅读
- typescript - typescript 基于操作静态扩展类型(类型安全)
- python - 页面布局的Python selenium打印页面问题
- python - 如何通过网络检查两个目录是否包含相同的文件?
- python - 如何检查列的 str 值,确定另一列是否小于/大于 [x] 在新建列中返回布尔值
- vulkan - 使 Vulkan 内存无效和刷新的正确方法
- sql - t-sql 在组内重复行号
- python - 通过 JSON 属性隔离字符串
- python - 如何使用 glob 获取 2018 年之后修改的 csv 文件列表?
- javascript - 如何让按钮显示我在网页输入框中输入的内容
- python - 如何使用python在while循环中仅发送一次电报机器人消息