0

所以我有一个程序,它需要一个 txt 文件并被读入并使用另一个 java 文件的类来设置数组。我正在尝试使用选择排序对放入数组中的值进行排序,但它为我提供了错误的操作数类型:(如果数组 [j] < 数组 [min])。使用的文本文件为:“Country”“Total CO2 2005(百万吨)”“Road CO2(百万吨)”“Road CO2 per person(吨)”“Cars per 1000 people”10 USA 5951.13 1530.3 5.16 777 UK 2573.4 119.68 1.99 470 Italy 476.08 116.86 2 592 Germany 841.78 150.21 1.82 550 Canada 553.02 123.42 3.82 562 France 414.03 128.13 2.04 477 Russia 1575.44 114.69 0.8 178 Japan 1254.47 224.24 1.76 447 China 5100.6 228.02 0.3 17 India 1147.46 91.06 0.1 8

类名为 carbonDioxide.java 的程序:

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;
    }
}

我正在编写的程序调用上述两个,CarbonAnalysis.java:

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

public class CarbonAnalysis {
    public static void main(String[]args){
        //CarbonDioxideData c1 = new CarbonDioxideData();
        //c1.setCountry("canada");
        //System.out.println(c1);
        Scanner userInput = new Scanner(System.in);
        String fileName ="";
        File inputFile = null;
        Scanner fileReader = null;
        while(fileReader==null){
        try{
        System.out.println("Enter input file name:");
        fileName= userInput.next();
        inputFile = new File(fileName);
        fileReader = new Scanner(inputFile);
        System.out.println("Successfully opening " + fileName);
        }catch(IOException err){
            System.out.println("Something went wrong");
            System.out.println(err);
            System.out.println("Please retry");
        }
        }

        String testLine = fileReader.nextLine();
        System.out.println(testLine);

        int numberOfEntries = fileReader.nextInt();
        System.out.println(numberOfEntries);

        CarbonDioxideData[] array = new CarbonDioxideData[numberOfEntries];
        for(int i =0;i<numberOfEntries;i++){


        CarbonDioxideData c1 = new CarbonDioxideData();
        String country = fileReader.next();
        c1.setCountry(country);
        double totalCO2 = fileReader.nextDouble();
        c1.setTotalCO2(totalCO2);
        double roadCO2 = fileReader.nextDouble();
        c1.setRoadCO2(roadCO2);
        double perPerson = fileReader.nextDouble();
        c1.setCO2PerPerson(perPerson);
        int cars = fileReader.nextInt();
        c1.setCarsPerPerson(cars);
        //System.out.println(c1);
        array[i]=c1;
    }
    printArray(array);
    emissionStats(array);
    }

    public static void printArray(CarbonDioxideData[] a){
        for(int i=0; i<a.length;i++){
            System.out.println(a[i]);
        }
    }

    public static void emissionStats(CarbonDioxideData[] array){
        for (int i = 0; i < array.length - 1; i++) { 
 // find index of smallest remaining value 
            int min = i; 
            for (int j = i + 1; j < array.length; j++) { 


                if (array[j] < array[min]) { 
                min = j; 
                } 
            } 

 // swap smallest value its proper place, a[i] 
            swap(array, i, min); 
        } 

    }

    public static void swap(CarbonDioxideData[] a, int i, int j) { 
        if (i != j) { 
            CarbonDioxideData temp = a[i]; 
            a[i] = a[j]; 
            a[j] = temp; 
        } 
    }    
}

编译时收到的错误:CarbonAnalysis.java:68: error: bad operand types for binary operator '<' if array[j] < array[min]

第一种:二氧化碳数据第二种:二氧化碳数据

我很茫然,我不知道如何让它工作。任何帮助表示赞赏

4

1 回答 1

0

<,>二元运算符可以与原始类型一起使用。

public class CarbonDioxideData implements Comparable<CarbonDioxideData> {

   private String country;

   private double totalCO2;

   private double roadCO2;

   private double CO2PerPerson;

   private int carsPerPerson;


   @Override
   public int compareTo(CarbonDioxideData that) {
    final int BEFORE = -1;
    final int EQUAL = 0;
    final int AFTER = 1;


    if (this == that) return EQUAL;

        //Compare function according to your logic
    if (this.totalCO2 == that.totalCO2) return EQUAL;
    if (this.totalCO2 > that.totalCO2) 
            return AFTER;
    else 
            return BEFORE;
  }

}

你的比较应该是

if (array[j].compareTo(array[min]) < 0) { 
    min = j; 
} 
于 2014-04-04T09:16:23.290 回答