0

so I have a program that takes a txt file and is read in and uses the class of another java file to set up the array. I am trying use selection sort to sort the values placed into the array but it gives me bad operand types for the line: (if array[j] < array[min]). The text file used is:"Country" "Total CO2 2005 (million tonnes)" "Road CO2 (million tonnes)" "Road CO2 per person (tonnes)" "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

The program with the class being called 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;
    }
}

The program I am writing calling the two above, 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; 
        } 
    }    
}

The error I am receiving when compiling: CarbonAnalysis.java:68: error: bad operand types for binary operator '<' if array[j] < array[min]

first type: CarbonDioxideData Second type: CarbondDioxidedata

I am at a loss I have no idea how to get it to work. Any help appreciated

1 Answers1

0

< , > binary operators can be used with primitive types.

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

}

Your comparison should be as

if (array[j].compareTo(array[min]) < 0) { 
    min = j; 
} 
Cahit Gungor
  • 1,477
  • 23
  • 30