【问题标题】:How to read a text file with multiple data types and store them in an Array?如何读取具有多种数据类型的文本文件并将它们存储在数组中?
【发布时间】:2022-01-16 09:01:23
【问题描述】:

我正在尝试读取一个包含各种数据的文本文件并将其存储到一个数组中,以便在接收到用户输入后我可以比较它以查看数组内是否有任何匹配项。

示例文本:

1A 1st true false false 28.50 free  
2B 2nd false true false 25.00 free  
3C 3rd true true false 32.50 free

如您所见,我正在处理字符串、布尔值和双精度值。

我最初尝试读取文件并将每一行作为字符串存储在字符串数组中,但是当我需要比较用户输入时,这不起作用。

然后我尝试创建一个数组和一个子数组,如下所示:

    try {
        File read = new File("seats.txt");
        Scanner reader = new Scanner(read);
        while (reader.hasNextLine()) {
            String dataRead = reader.nextLine();
            
            String[] seatData = dataRead.split(" ");
            
            String seatID = seatData[0];
            String seatClass = seatData[1];
            boolean window = Boolean.parseBoolean(seatData[2]);
            boolean aisle = Boolean.parseBoolean(seatData[3]);
            boolean table = Boolean.parseBoolean(seatData[4]);
            double seatPrice = Double.parseDouble(seatData[5]);
            String seatFree = seatData[6];
            
            Seats info = new Seats(seatID, seatClass, window, aisle, table, seatPrice, seatFree);
            result = info;
            System.out.println(result); }
        
            reader.close();
            
    }catch (FileNotFoundException e) {
        System.out.println("An error has occurred.");
        e.printStackTrace(); }
    return result; 
    
}

这是正确的方法还是我应该尝试其他方法?

任何见解或建议将不胜感激。谢谢。

【问题讨论】:

  • 这很好,但您没有存储您阅读的内容?你只需打印它
  • result 类型是什么?
  • 我知道这有点超出范围,但为什么不使用 CSV 呢?

标签: java arrays


【解决方案1】:

你阅读和解析很好,但你没有保存任何东西,你一直用最后一个info覆盖result,所以你的方法只返回一个Seats实例,最后一个,使用@987654325 @ 轻松添加您创建的所有席位

有一些改进

static List<Seat> read() {
    List<Seat> results = new ArrayList<>();
    File read = new File("seats.txt");
    String dataRead;

    try (BufferedReader reader = new BufferedReader(new FileReader(read))) {
        while ((dataRead = reader.readLine()) != null) {
            String[] seatData = dataRead.split(" ");
            String seatID = seatData[0];
            String seatClass = seatData[1];
            boolean window = Boolean.parseBoolean(seatData[2]);
            boolean aisle = Boolean.parseBoolean(seatData[3]);
            boolean table = Boolean.parseBoolean(seatData[4]);
            double seatPrice = Double.parseDouble(seatData[5]);
            String seatFree = seatData[6];
            Seat info = new Seat(seatID, seatClass, window, aisle, table, seatPrice, seatFree);
            System.out.println(info);
            results.add(info);
        }
    } catch (FileNotFoundException e) {
        System.out.println("An error has occurred.");
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return results;
}

【讨论】:

  • 我已经有一个 Seats 类,我尝试使用构造函数使每个单独的“Seat”成为一个对象。但我无法让它正常工作。
  • @NM15 哼,我的代码是否适合你,我不明白?
  • 对不起,它确实有效。非常感谢您的帮助。
  • @NM15 所以你可以考虑投票/接受答案;)在我的答案左侧投票和绿色
  • 我试过了,但由于我的声誉很低,我认为它没有显示出来。
【解决方案2】:

使用杰克逊。 https://cowtowncoder.medium.com/reading-csv-with-jackson-c4e74a15ddc1。请参见倒数第二个示例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-26
    • 1970-01-01
    • 2021-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多