Javaの質問です。
データに0より小さい数値がある場合、その行を取り除いて集計させ、エラーがある行だけを別ファイルに書きだしたいのですが、うまくいきません。
なにかアドバイスいただけますでしょうか?
よろしくお願いします。
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
class Data {
double value;
Data(double value) {
this.value = value;
}
void setData(double value) {
this.value = value;
}
public String toString() {
return String.format("%5.1f", value);
}
}
class Statistics {
Data min = new Data(999.9);
Data max = new Data(0.0);
double total = 0.0;
void add(double value) {
if (min.value > value) {
min.setData(value);
}
if (max.value < value) {
max.setData(value);
}
total += value;
}
}
class Category {
String category;
Statistics height = new Statistics();
Statistics weight = new Statistics();
Statistics bmi = new Statistics();
int num = 0;
Category(String category) {
this.category = category;
}
void add(double height, double weight) {
this.height.add(height);
this.weight.add(weight);
this.bmi.add(weight / (height / 100.0 * height / 100.0));
num++;
}
public String toString() {
return category + "," + "身長" + "," + "最高" + "," + height.max + "\n"
+ category + "," + "身長" + "," + "最低" + "," + height.min + "," + "\n"
+ category + "," + "身長" + "," + "平均" + "," + String.format("%5.1f\n", height.total / num)
+ category + "," + "体重" + "," + "最高" + "," + weight.max + "," + "\n"
+ category + "," + "体重" + "," + "最低" + "," + weight.min + "\n"
+ category + "," + "体重" + "," + "平均" + "," + String.format("%5.1f\n", weight.total / num)
+ category + "," + "BMI" + "," + "最高" + "," + bmi.max + "\n"
+ category + "," + "BMI" + "," + "最低" + "," + bmi.min + "\n"
+ category + "," + "BMI" + "," + "平均" + "," + String.format("%5.1f\n", bmi.total / num);
}
}
class Health {
Category male = new Category("男");
Category famale = new Category("女");
Category total = new Category("全体");
void add(String sex, double height, double weight) {
if (sex.equals("男")) {
male.add(height, weight);
} else {
famale.add(height, weight);
}
total.add(height, weight);
}
public String toString() {
return "" + male + "\n" + famale + "\n" + total + "\n";
}
}
public class Main {
static final String FILENAME = "hw.csv";
static final String SEPARATOR = ",";
static final int SEX = 2, HEIGHT = 3, WEIGHT = 4;
public static void main(String[] args) throws Exception {
Health health = new Health();
String line;
BufferedReader file = new BufferedReader(new FileReader(FILENAME));
for (int number = 1; (line = file.readLine()) != null; number++) {
String[] item = line.split(SEPARATOR);
try {
if(Integer.parseInt(item[HEIGHT]) <= 0 || Integer.parseInt(item[WEIGHT]) <= 0 ||
item[HEIGHT] == null || item[WEIGHT] == null)
{
PrintWriter pw = new PrintWriter( new BufferedWriter (new FileWriter("out_error.csv")));
pw.println(number);
pw.close();
health.add(item[SEX],Double.parseDouble(item[HEIGHT]),Double.parseDouble(item[WEIGHT]));
}
} catch (Exception e) {
}
}
file.close();
PrintWriter pw = new PrintWriter( new BufferedWriter (new FileWriter("out.csv")));
pw.println(health);
pw.close();
}
}
お礼
あ、そういう事でしたか。 素直にStrToIntする事にします。 ありがとうございました。