0

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());
    }
}
Yihan Tang
  • 11
  • 1

1 Answers1

1

Why scanner? java.nio.Files api is much better for understanding (https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html).

Read whole file to List<String> where each string is line from file. Then create simple model & split each row by "," to:

"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"

if you don't need any field just skip it (but i see exception handling so probably you need them all to be validated)

Then when List yourList exists, just sort objects in your collection by given field and take minimum and maximum.

notAPPP
  • 188
  • 5