当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Printing out specific data and calculations when multiple records in a CSV file

问题描述:

 import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class CSVReader {

public static void main(String[] args) {

CSVReader obj = new CSVReader();

obj.run();

}

public void run() {

String CSVFile = "/Users/Wyatt/CSVFile2.csv";

BufferedReader br = null;

String Line = "";

String analysisStr;

String CSVDelimiter = ",";

int analysis;

double AVGDate = 0;

BufferedReader console = new BufferedReader(new InputStreamReader(System.in));

try {

br = new BufferedReader(new FileReader(CSVFile));

while ((Line = br.readLine()) != null) {

String[] Weather = Line.split(CSVDelimiter);

System.out.println("Weather [Date: "

+ Double.parseDouble(Weather[0]) + ", Interval: "

+ Double.parseDouble(Weather[1]) + ", Rainfall: "

+ Double.parseDouble(Weather[2]) + ", Sunlight: "

+ Double.parseDouble(Weather[3]) + ", Time: "

+ Double.parseDouble(Weather[4]) + ", Total: "

+ Double.parseDouble(Weather[5]) + ", Overall: "

+ Double.parseDouble(Weather[6]) + "]");

AVGDate = (Double.parseDouble(Weather[0]) / 804);

System.out.println(AVGDate);

}

System.out.println("What data is wanted?");

analysisStr = console.readLine();

analysis = Integer.parseInt(analysisStr);

if(analysis == 0) {

System.out.println("Date.");

System.out.println("Minimum date is:");

System.out.println("Maximum date is:");

System.out.println("Average date is: " + AVGDate);

}

else if(analysis == 1) {

System.out.println("Interval.");

System.out.println("This data isn't useful.");

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

if (br != null) {

try {

br.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

System.out.println("Done.");

}

}

Heading

The program above reads the file correctly, reads and prints out all the records but for my average date it is calculating each individual records date instead of calculating all the dates together, I am a novice user and really new to Java, I can't see what I am doing wrong. The other problem is that I don't know how to calculate the minimum/maximum data for each column. Just straight up not sure on how to do this.

网友答案:

To calculate average you have to first calculate the total and divide it by number of elements. So initialize total and sum it up in while loop every time you read a data.

double total = 0.0

while(...) {
    ...
    total += Double.parseDouble(Weather[0]);
}
AVGDate = total / 804;

Now about calculating minimum or maximum...

double minDate = Double.MAX_VALUE;
double maxDate = Double.MIN_VALUE;
while(...) {
    // parse each data
    double temp = Weather[0];    
    if(temp < minDate) {
        minDate = temp;
    }

    else if(temp > maxDate) {
        maxDate = temp;
    }
}

also keep in mind we use lower-case convention for non final variable naming.

分享给朋友:
您可能感兴趣的文章:
随机阅读: