首页 > 解决方案 > 记录数组和文件处理

问题描述

我试图弄清楚为什么我的文件不会输出。它是一个数据文件,包括显示器及其刷新率、响应时间、显示器型号和品牌。每当我运行它时,我都会遇到 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

标签: javaarraysfile-io

解决方案


问题代码不断变化,因此无法 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目录中,所以上面的示例是更正确的读取方式。


推荐阅读