使用CSVParser从CSV文件中查找平均温度

时间:2017-02-09 10:02:44

标签: java csv if-statement

我已经测试了代码,它可以根据需要运行。但是,如果从testaverageTemperatureWithHighHumidityInFile()调用averageTemperatureWithHighHumidityInFile()时currRow是>> =值,我希望能够打印System.out.println("没有温度和#34;)。我该怎么做呢?

         public double averageTemperatureWithHighHumidityInFile(CSVParser parser, int value){
         double averageTemperatureWithHighHumidity =0.0;
         int numberOfDays = 0;
         double sum =0;
         double averageTemp = 0;

`             for (CSVRecord currentRow : parser){

            double currRow = Double.parseDouble(currentRow.get("Humidity"));
              if (currRow >= value){


                  double temp = Double.parseDouble(currentRow.get("TemperatureF"));

                   sum +=temp;
                   numberOfDays++;

                } 
                averageTemp = sum/numberOfDays;            
            }
    return averageTemp;

    } 




    public void testAverageTemperatureWithHighHumidityInFile(){
            FileResource fr = new FileResource();      
            double testAverageTemperatureWithHighHumidityInFile = averageTemperatureWithHighHumidityInFile(fr.getCSVParser(),80);
           System.out.println("Average temperature when high humidity is " +  testAverageTemperatureWithHighHumidityInFile );

    }

1 个答案:

答案 0 :(得分:0)

public double averageTemperatureWithHighHumidityInFile(CSVParser parser, int value){
     double averageTemperatureWithHighHumidity =0.0;
     int numberOfDays = 0;
     double sum =0;
     double averageTemp = 0;
     for (CSVRecord currentRow : parser){

        double currRow = Double.parseDouble(currentRow.get("Humidity"));
          if (currRow >= value){


              double temp = Double.parseDouble(currentRow.get("TemperatureF"));

               sum +=temp;
               numberOfDays++;

            } else{

            System.out.println("Average temperature with that humidity does not exisi");
            }
                averageTemp = sum/numberOfDays;   
        } 
return averageTemp;

}