This question already has answers here:
CSV parsing in Java - working example..? [closed] (10 answers)
parsing a csv file using java (3 answers)
Java Stream: find an element with a min/max value of an attribute (9 answers)
Fast way to get the min/max values among properties of object (16 answers)
Closed last month.
For this assignment, I need two java classes to find the max and min temperatures in a CSV file. Using the command line like this:
$ java TemperatureStats Temperatures_Feb2021.csv
output example:
$ 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
here is the instruction for those two classes
TemperatureParser.java:
• Constructor: passed the filename as a parameter. The constructor then creates a scanner object, reads the header, and finds which column contains the "Date/Time (LST)” string and which column contains the "Temp (C)” string. This will allow extraction of the date/time string and temperature values for each line. The scanner and the two column numbers are stored as instance variables.
• parseLine(): parses (tokenizes) a line of a csv weather file, using the scanner object created by the constructor (instance variable). It finds the date/time string and the temperature and stores them in instance variables. This method finds these values using the columns identified by the constructor. Note: each time the parseLine method is called the date/time string and the temperature instance data will be overwritten with the values from the next line of data.
• Accessor methods: to retrieve the date/time string, the temperature, and determine whether there are any more lines in the file.
You also may want to create private helper methods.
TemperatureStats.java:
with a main method that does the following:
• Creates an instance of TemperatureParser using the filename passed in as a command line argument. If the filename is missing from the command line, then the program terminates with the message: Usage: java TemperatureStats <file.csv>
• Parses each line of the file using the parseLine() method (and the accessor methods) of TemperatureParser and records and prints the minimum and maximum temperatures, along with the associated date/time string.
• Handles the two kinds of exceptions described above: incomplete line or missing value (empty string instead), by printing a message identifying the line where the problem occurs (OK to assume that the date/time string will always be there), as in the examples above.
• Handles IOExceptions (e.g.: FileNotFoundException) by printing an appropriate message and ending the program.
here is sample 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"
here is exception handle requirment:
Note: the maximum and minimum temperatures are printed along with the date and time they occurred. Data may be incomplete. Your program must handle two possibilities. First, a line from the csv file may be incomplete, such as in the file for February 2021, where the weather data ends on "2021-02-15 23:00”, but the remaining hours to the end of the month appear in the file with the weather data missing. Here’s how your program should handle this:
$ 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
Note that date and time for each line with missing data is identified (not all data is shown here to keep the output short for the assignment document). The second way data may be incomplete is that a value may be missing, (an empty string, i.e. " ") shows up in the temperature column, instead a value (such as "-14.3"). In the following example, the first temperature in the January file was removed and replaced with " " (and saved in a file called Temperatures_Jan2021_Test.csv), which is handled in the same way as the first issue by identifying the line with an invalid entry:
$ 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
I'm trying to get the output right first, so I'm not adding exceptions. The problem I am having is that the csv file cannot be read this is my code
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());
}
}
1条答案
按热度按时间gab6jxml1#
为什么要使用scanner?java.nio.Files api更便于理解(https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html)。
将整个文件读取到
List<String>
,其中每个字符串都是文件中的行。然后创建简单模型,并将每行按“”拆分为:“经度(x)"",纬度(y)",“站点名称”,“气候ID”,“日期/时间(LST)"、“年”、“月”、“日”、“时间(LST)",温度(C)",“温度标志”,“露点温度(°C)",“露点温度标志”,“相对湿度(%)",“发货量标志”,“扣除金额(mm)",“精确量标志”,“风向(10 s deg)"、“风向标志”、“风速(km/h)"、“风速标志”、“能见度(km)"、“能见度标志”、“气压(kPa)"、”气压标志”、“Hmdx”、“Hmdx标志”、“风寒”、“风寒标志”、“天气”
如果您不需要任何字段,则跳过它(但我看到异常处理,因此可能您需要验证所有字段)
然后,当List yourList存在时,只需按给定字段对集合中的对象进行排序,并取最小值和最大值。