【问题标题】:Sorting arrays from a file and printing results从文件中排序数组并打印结果
【发布时间】:2014-04-02 07:46:08
【问题描述】:

我必须读取一个数据文件并将其存储到一个整数数组中,对数组进行排序,然后报告最高总数和最低总数,但由于某种原因,当我运行我的代码时什么都没有出现,它说它没有错误。这是我到目前为止的代码......

import java.io.*;
import java.util.Scanner;

public class CarbonAnalysis {
public static void main (String [] Args) throws FileNotFoundException {

    Scanner s = new Scanner(System.in);
    File f = new File("carbon_data.txt");
    Scanner welcome = new Scanner(f);
    File outputFile = new File("carbon_report.txt");
    PrintStream output = new PrintStream(outputFile);
    String firstLine = welcome.nextLine();
    int secondLine = welcome.nextInt();

    CarbonDioxideData[] Country = new CarbonDioxideData[secondLine];

    for(int i = 0; i < secondLine; i++) {
        Country[i] = new CarbonDioxideData();
        Country[i].setCountry(welcome.next());
        Country[i].setTotalCO2(welcome.nextDouble());
        Country[i].setRoadCO2(welcome.nextDouble());
        Country[i].setCO2PerPerson(welcome.nextDouble());
        Country[i].setCarsPerPerson(welcome.nextInt());
    }
    int count = 0;
    int count2 = 0;
    CarbonDioxideData[] totalEmissions = new CarbonDioxideData[count];
    CarbonDioxideData[] perPersonRoadEmissions = new CarbonDioxideData[count2];

    reportDescription(output);
    sortTotalEmissions(totalEmissions);
    sortPerPersonRoadEmissions(perPersonRoadEmissions);

}
//prints the output of data analyzed
public static void reportDescription(PrintStream output) {
    output.println("Country with the lowest total emissions: ");
    output.println("Country with the highest total emissions: " );
    output.println("Canada is ranked for lowest total emissions.");
    output.println();
    output.println("Country with the lower per-person road emissions: ");
    output.println("Country with the highest per-person road emissions: ");
    output.println("Canada is ranked for the lowest per-road emissions.");
}
//sorts the total Emissions from highest to lowest
public static void sortTotalEmissions(CarbonDioxideData[] totalEmissions){
    for(int i = 0; i < totalEmissions.length; i++) {
        double max = totalEmissions[i].getTotalCO2();
        int maxPos = i;
        for(int j = i; j < totalEmissions.length; j++) {
            if(max < totalEmissions[j].getTotalCO2() ) {
                max = totalEmissions[j].getTotalCO2();
                maxPos = j;
            }
        }
        CarbonDioxideData temp = totalEmissions[maxPos];
        totalEmissions[maxPos] = totalEmissions[i];
        totalEmissions[i] = temp;
    }
}
//sorts the per person road Emissions from highest to lowest
public static void sortPerPersonRoadEmissions(CarbonDioxideData[] perPersonRoadEmissions){
    for(int i = 0; i < perPersonRoadEmissions.length; i++) {
        int max = perPersonRoadEmissions[i].getCarsPerPerson();
        int maxPos = i;
        for(int j = i; j < perPersonRoadEmissions.length; j++) {
            if(max < perPersonRoadEmissions[j].getCarsPerPerson() ) {
                max = perPersonRoadEmissions[j].getCarsPerPerson();
                maxPos = j;
            }
        }
        CarbonDioxideData temp = perPersonRoadEmissions[maxPos];
        perPersonRoadEmissions[maxPos] = perPersonRoadEmissions[i];
        perPersonRoadEmissions[i] = temp;
    }
}

}

提供给我帮助的代码:

public class CarbonDioxideData {


private String country;

private double totalCO2;    

private double roadCO2;

private double CO2PerPerson;

private int carsPerPerson;

public CarbonDioxideData() {
    country = "blank_country";
    totalCO2 = -1.0;
    roadCO2 = -1.0;
    CO2PerPerson = -1.0;
    carsPerPerson = -1; 
}

public String toString() {
    String result = country;

    result += " " + totalCO2;
    result += " " + roadCO2;
    result += " " + CO2PerPerson;
    result += " " + carsPerPerson;

    return result;
}
public String getCountry() {
    return country;
}

public void setCountry(String country) {
    this.country = country;
}

public double getTotalCO2() {
    return totalCO2;
}

public void setTotalCO2(double totalCO2) {
    this.totalCO2 = totalCO2;
}

public double getRoadCO2() {
    return roadCO2;
}

public void setRoadCO2(double roadCO2) {
    this.roadCO2 = roadCO2;
}

public double getCO2PerPerson() {
    return CO2PerPerson;
}

public void setCO2PerPerson(double cO2PerPerson) {
    CO2PerPerson = cO2PerPerson;
}
public int getCarsPerPerson() {
    return carsPerPerson;
}

public void setCarsPerPerson(int carsPerPerson) {
    this.carsPerPerson = carsPerPerson;
}

}

【问题讨论】:

  • 能贴出sortTotalEmissionssortPerPersonRoadEmissions的实现吗?
  • 你永远不会刷新或关闭你的流。
  • @ArnaudDenoyelle 我该如何关闭它?
  • @user3461630 在程序结束时调用output.close()

标签: java arrays sorting


【解决方案1】:

您的程序总是在文件中输出一个常量字符串,因为 reportDescription 中没有变量。

其次,为了正确排序数组,CarbonDioxideData 应该实现Comparable。然后你可以打电话 Arrays.sort 像这样:

Arrays.sort(perPersonRoadEmissions)

然后检索最高/值:

perPersonRoadEmissions[0] / perPersonRoadEmissions[perPersonRoadEmissions.length-1]

要显示信息,您必须在排序后调用输出,并将签名更改为接受最高和最低值的变量。

Arrays.sort(totalEmissions);
Arrays.sort(perPersonRoadEmissions)
reportDescription(output,totalEmissions[0],totalEmissions[totalEmissions.length-1],perPersonRoadEmissions[0],perPersonRoadEmissions[perPersonRoadEmissions.length-1]);

作为替代方案,您也可以简单地将数组作为参数传递并在 reportDescription 的正文中检索 min max:

 reportDescription(output,totalEmissions,perPersonRoadEmissions);

如何更改reportDescription的内容和实现compareTo留给OP。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-09
    • 2020-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多