我正在开发一个使用外部文件的程序。该计划必须: 编写程序将调查结果读入三个数组,回显打印数据并执行并输出以下分析:
统计调查中包含的住户数量并打印显示所读数据的三栏表(假设不超过25户)
计算家庭平均收入,并列出每个家庭的识别号码和收入超过平均值。
确定收入低于贫困水平的家庭百分比。贫困水平收入可使用以下公式计算:p = $ 3750.00 + $ 750.00 *(m-2)其中m是每个家庭的成员数。
这是我到目前为止所做的,但它似乎不起作用(我必须要上课):
import java.io.*;
import java.util.Scanner;
public class main
{
public static void main(String[] args) throws Exception
{
Scanner sf = new Scanner(new FileInputStream("survey2.dat"));
String[] filePaths = { "D:/survey2.dat" };
SurveyCollection surveys = new SurveyCollection();
Runner runner = new Runner();
runner.load(filePaths, surveys);
runner.printHouseholdIncomeExceedingAverage(surveys);
runner.printouseholdBelowPoverty(surveys);
//print(surveys);
System.exit(0);
}
}
import java.io.*;
import java.util.Scanner;
public class Runner
{
public static void load(String[] filePaths, SurveyCollection surveys)throws Exception {
for (int i = 0; i < filePaths.length; i++) {
Scanner fileIn = null;
try {
fileIn = new Scanner(new FileReader(filePaths[i]));
while (fileIn.hasNextLine()) {
String line = fileIn.nextLine();
surveys.setSurvey(line);
}
} catch (Exception ex) {
ex.printStackTrace();
} /*finally {
fileIn.close();
}*/
}
}
public static void printHouseholdIncomeExceedingAverage(
SurveyCollection surveys) {
int size = surveys.getNumberOfHousehold();
double average = surveys.getAverageIncome();
String message = "Households with income exceeding an average income of %,.2f%n";
System.out.printf(message, average);
System.out.println("Id Income Member");
for (int i = 0; i < size; i++) {
if (surveys.getIncomeByIdx(i) > average) {
System.out.println(surveys.formattedString(i));
}
}
}
/**
* design for debug
* @param surveys
*/
private static void print(SurveyCollection surveys) {
int size = surveys.getNumberOfHousehold();
System.out.println();
for (int i = 0; i < size; i++) {
System.out.println(surveys.formattedString(i));
}
}
public static void printouseholdBelowPoverty(SurveyCollection surveys) {
int size = surveys.getNumberOfHousehold();
int count = 0;
System.out.println();
for (int i = 0; i < size; i++) {
if (surveys.isInPoverty(i)) {
System.out.println(surveys.formattedString(i));
count++;
}
}
String message = "Percent of households below poverty level = %f%n";
System.out.printf(message, ((double) count / (double) size));
}
}
class SurveyCollection {
final int MAX = 30;
private int idx = 0;
private int sumOfIncome = 0;
private String[] surveyIds = new String[MAX];
private int[] incomes = new int[MAX];
private int[] members = new int[MAX];
public void setSurvey(String value) {
if (idx < MAX) {
String[] fields = value.split(" ");
surveyIds[idx] = fields[0];
incomes[idx] = Integer.parseInt(fields[1]);
members[idx] = Integer.parseInt(fields[2]);
sumOfIncome += incomes[idx];
idx++;
}
}
public int getNumberOfHousehold() {
return this.idx;
}
public double getAverageIncome() {
return (double) sumOfIncome / getNumberOfHousehold();
}
public int getIncomeByIdx(int idx) {
return incomes[idx];
}
public String formattedString(int idx) {
String style = "%s %d %d";
return String.format(style, this.surveyIds[idx], this.incomes[idx], this.members[idx]);
}
public boolean isInPoverty(int idx) {
boolean result = false;
int threadhold = (3750 + 750 * members[idx]);
result = incomes[idx] <= threadhold;
return result;
}
}
我做错了什么?
外部文件:
1041 12180 4
1062 13240 3
1327 19800 2
1483 22458 8
1900 17000 2
2112 18125 7
2345 15623 2
3210 3200 6
3600 6500 5
3601 11970 2
4725 8900 3
6217 10000 2
9280 6200 1
输出应与此类似:
id income members
1041 12180 4
1062 13240 3
1327 19800 2
1483 22458 8
1900 17000 2
2112 18125 7
2345 15623 2
3210 3200 6
3600 6500 5
3601 11970 2
4725 8900 3
6217 10000 2
9280 6200 1
Households with income exceeding an average income of 12707.4
id income members
1062 13240 3
1327 19800 2
1483 22458 8
1900 17000 2
2112 18125 7
2345 15623 2
Percent of households below poverty level = 0.0769231
答案 0 :(得分:0)
当我将整个文件路径传递给runner.load方法时,我得到一些输出。
String fileName = "survey2.dat";
String filePaths[] = {"C:\\Users\\Neal\\Desktop\\" + fileName};
Households with income exceeding an average income of 12,707.38
Id Income Member
1062 13240 3
1327 19800 2
1483 22458 8
1900 17000 2
2112 18125 7
2345 15623 2
3210 3200 6
3600 6500 5
Percent of households below poverty level = 0.153846