在 Java 的 csv 文件中查找 max ,min [duplicate]

find max ,min in csv file in Java [duplicate]

提问人:Yihan Tang 提问时间:10/19/2022 更新时间:10/19/2022 访问量:193

问:

对于此作业,我需要两个 java 类来查找 CSV 文件中的最高和最低温度。 像这样使用命令行:

$ java TemperatureStats Temperatures_Feb2021.csv

输出示例:

$ java TemperatureStats Temperatures_Jan2021.csv
Here is the output of the program for the first file:
Maximum: 2021-01-15 15:00: 3.1
Minimum: 2021-01-31 08:00: -19.3

这是这两个类的说明

TemperatureParser.java:

• 构造函数:将文件名作为参数传递。然后,构造函数创建一个扫描程序 对象,读取标头,并查找包含“日期/时间 (LST)”字符串的列 以及哪一列包含“Temp (C)”字符串。这将允许提取 每行的日期/时间字符串和温度值。扫描仪和两列 数字存储为实例变量。

• parseLine():使用 scanner 对象解析(标记化)csv 天气文件的一行 由构造函数(实例变量)创建。它查找日期/时间字符串和 温度并将它们存储在实例变量中。此方法使用以下方法查找这些值 由构造函数标识的列。注意:每次调用 parseLine 方法时 日期/时间字符串和温度实例数据将被 下一行数据中的值。

• 访问器方法:检索日期/时间字符串、温度并确定 文件中是否还有更多行。

您可能还希望创建私有帮助程序方法。

TemperatureStats.java:

使用执行以下操作的 main 方法:

• 使用作为命令传入的文件名创建 TemperatureParser 的实例 line 参数。如果命令行中缺少文件名,则程序 终止消息: 用法:java TemperatureStats <file.csv>

• 使用 parseLine() 方法(和访问器方法)解析文件的每一行 并记录和打印最小值和最大值 温度,以及关联的日期/时间字符串。

• 处理上述两种异常:行不完整或值缺失 (代替空字符串),通过打印一条消息来标识问题所在行 occurs (可以假设日期/时间字符串将始终存在),如示例所示 以上。

• 通过打印适当的 IOExceptions(例如:FileNotFoundException)来处理 IOExceptions(例如:FileNotFoundException) 消息并结束程序。

以下是示例Temperatures_Feb2021.csv:

"Longitude (x)","Latitude (y)","Station Name","Climate ID","Date/Time (LST)","Year","Month","Day","Time (LST)","Temp (C)","Temp Flag","Dew Point Temp (°C)","Dew Point Temp Flag","Rel Hum (%)","Rel Hum Flag","Precip. Amount (mm)","Precip. Amount Flag","Wind Dir (10s deg)","Wind Dir Flag","Wind Spd (km/h)","Wind Spd Flag","Visibility (km)","Visibility Flag","Stn Press (kPa)","Stn Press Flag","Hmdx","Hmdx Flag","Wind Chill","Wind Chill Flag","Weather"
"-66.54","45.87","FREDERICTON INTL A","8101505","2021-02-01 00:00","2021","02","01","00:00","-14.2","","-16.2","","85","","0.0","","20","","5","","16.1","","102.42","","","","-18","","NA"
"-66.54","45.87","FREDERICTON INTL A","8101505","2021-02-01 01:00","2021","02","01","01:00","-14.5","","-16.4","","86","","0.0","","19","","4","","16.1","","102.44","","","","-17","","NA"
"-66.54","45.87","FREDERICTON INTL A","8101505","2021-02-01 02:00","2021","02","01","02:00","-15.0","","-16.7","","87","","0.0","","","","0","","16.1","","102.46","","","","","","NA"
"-66.54","45.87","FREDERICTON INTL A","8101505","2021-02-01 03:00","2021","02","01","03:00","-16.0","","-17.7","","87","","0.0","","19","","8","","16.1","","102.50","","","","-22","","NA"
"-66.54","45.87","FREDERICTON INTL A","8101505","2021-02-01 04:00","2021","02","01","04:00","-16.1","","-17.7","","88","","0.0","","21","","5","","16.1","","102.50","","","","-20","","NA"
"-66.54","45.87","FREDERICTON INTL A","8101505","2021-02-01 05:00","2021","02","01","05:00","-17.0","","-18.8","","86","","0.0","","20","","5","","16.1","","102.50","","","","-21","","NA"
"-66.54","45.87","FREDERICTON INTL A","8101505","2021-02-01 06:00","2021","02","01","06:00","-17.7","","-19.5","","86","","0.0","","26","","4","","16.1","","102.48","","","","-21","","NA"
"-66.54","45.87","FREDERICTON INTL A","8101505","2021-02-01 07:00","2021","02","01","07:00","-18.0","","-19.9","","85","","0.0","","20","","5","","16.1","","102.56","","","","-22","","NA"

以下是异常句柄要求:

注意:最高和最低温度与日期和时间一起打印 他们发生了。数据可能不完整。程序必须处理两种可能性。 首先,csv 文件中的一行可能不完整,例如在 2021 年 2 月的文件中, 天气数据在“2021-02-15 23:00”结束,但剩余小时结束 的月份出现在文件中,但缺少天气数据。以下是您的程序如何 应该处理这个:

$ java TemperatureStats Temperatures_Feb2021.csv
invalid entry at 2021-02-16 00:00
invalid entry at 2021-02-16 01:00
invalid entry at 2021-02-16 02:00
...
invalid entry at 2021-02-28 22:00
invalid entry at 2021-02-28 23:00
Maximum: 2021-02-03 16:00: 2.6
Minimum: 2021-02-10 08:00: -23.0

请注意,将标识缺少数据的每行的日期和时间(未显示所有数据) 此处使作业文档的输出简短)。第二种方式数据可能是 不完整是可能缺少一个值,(一个空字符串,即“ ”)显示在 temperature 列,而是一个值(例如“-14.3”)。在以下示例中,第一个 删除了 1 月文件中的温度,并将其替换为 “ ”(并保存在文件中 称为 Temperatures_Jan2021_Test.csv),其处理方式与第一个相同 通过标识具有无效条目的行来解决问题:

$ java TemperatureStats Temperatures_Jan2021_Test.csv
invalid entry at 2021-01-01 00:00
Maximum: 2021-01-15 15:00: 3.1
Minimum: 2021-01-31 08:00: -19.3

我正在尝试首先获得正确的输出,所以我没有添加异常。 我遇到的问题是无法读取csv文件 这是我的代码


import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

import static java.util.Arrays.stream;

public class TemperatureParser {
    String time;
    String temp;


    public TemperatureParser() {
        String time;
        String temp;
    }
    static TemperatureParser[] weatherList;

    public void setTime(String t) {
        this.time = t;
    }

    public void setTemp(String temp) {
        this.temp = temp;
    }

    public String getTemp() {
        return temp;
    }

    public String getTime() {
        return time;
    }

    public static void parsing(String file) throws FileNotFoundException {

        Scanner sc = new Scanner(new File(file));
        Scanner valueScan = null;
        Scanner headScan = null;
        int index = 0;
        int ii=0;

        for (int i = 0; i < 30; i++) {
            headScan = new Scanner(sc.nextLine());
            headScan.useDelimiter(",");
            String useless =headScan.nextLine();
        }

        while (sc.hasNextLine()) {

            valueScan = new Scanner(sc.nextLine());
            valueScan.useDelimiter(",");
            TemperatureParser parser = new TemperatureParser();
            while (valueScan.hasNext()) {
                String data = valueScan.next();
                if (index == 5) {
                    parser.setTime(data);
                } else if (index == 10) {
                    parser.setTemp(data);
                }
                index++;
            }
            index = 0;
            weatherList[ii]=(parser);
            ii++;
        }
        sc.close();
    }
    public double[] getTempArray(){
        double[] tempArray = new double[weatherList.length];
        for (int i = 0; i < weatherList.length; i++) {
            tempArray[i]=Double.valueOf(weatherList[i].temp);
        }
        return tempArray;
    }
    public double max(){
        return stream(getTempArray()).max().getAsDouble();
    }
    public double min(){
        return stream(getTempArray()).min().getAsDouble();
    }
}

import java.io.FileNotFoundException;
public class TemperatureStats {
    public static void main(String[] args) throws FileNotFoundException {
        TemperatureParser parser = new TemperatureParser();
        parser.parsing(args[0]);
        System.out.println(parser.max());
        System.out.println(parser.min());
    }
}
Java CSV 文件 异常 IO

评论


答:

1赞 notAPPP 10/19/2022 #1

为什么选择扫描仪?java.nio.Files api 更适合理解 (https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html)。

将整个文件读取到每个字符串从文件中读取到其中的行。然后创建简单的模型,并用“,”拆分每一行,以:List<String>

“经度 (x)”,“纬度 (y)”,“站名”,“气候 ID”,“日期/时间” (LST)“,”Year“,”Month“,”Day“,”Time (LST)“,”Temp (C)“,”Temp Flag“,”Dew 点温度 (°C)“,”露点温度标志“,”Rel Hum (%)“,”Rel Hum Flag“,”沉淀。量 (mm)“,”降水.Amount Flag“,”Wind Dir (10s deg)“,”风向旗“,”风速(公里/小时)“,”风速旗“,”能见度 (km)“,”能见度标志“,”Stn Press (kPa)“,”Stn Press 旗帜“,”Hmdx“,”Hmdx 旗帜“,”风寒“,”风寒旗帜“,”天气”

如果您不需要任何字段,请跳过它(但我看到了异常处理,因此您可能需要对它们进行验证)

然后,当 List yourList 存在时,只需按给定字段对集合中的对象进行排序,并取最小值和最大值。