David Rolfe
David Rolfe

Reputation: 163

How do I write a method to determine the highest value in an array

The array is part of a class called CO2Data so the array is a CO2Data array. The problem is I'm trying to find the highest value in the array but my method isn't working. To get the values from the array the method must first get them from the CO2data class. So what's wrong with my method:

    public static CO2Data highest (CO2Data []  arr2){
Scanner sc = null;
CO2Data highestindex = arr2[0].getTotalCO2;
for (int i = 0; i<arr2.length; i++){
arr2[i].getTotalCO2(sc.nextDouble());
if(arr2[i].getTotalCO2() > highestindex ) {
    highestindex = arr2[i].getTotalCO2();
}
}
System.out.println(highestindex);
return highestindex;
}

Here's what the CO2data class looks like:

public class CO2Data {

private String country; //A private variable will prevent other users from accessng and chaning these variables. 
private double totalCO2;
private double roadCO2;
private double CO2PerPerson;
private int carsPerPerson;

public CO2Data() { 
    country = "";//this sets the initial values for the different variables
    totalCO2 = 0;
    roadCO2 = 0;
    CO2PerPerson = 0;
    carsPerPerson = 0;
}

public String getCountry() {
    return country;
}

public void setCountry(String country) { //to set the country you have to use the this command to access the private variable
    this.country = country; //you have to use this.country instead of just country because the string county has been declared as a private variable. 
}

public double getTotalCO2() {
    return totalCO2;
}

    public void setTotalCO2(double totalCO2) {
        this.totalCO2 = totalCO2; //The this.item command allows you to access private variables. 
    }

    public double getRoadCO2() {
        return roadCO2;
    }

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

    public double getCO2PerPerson() {
        return CO2PerPerson;
    }

    public void setCO2PerPerson(double cO2PerPerson) {
        this.CO2PerPerson = cO2PerPerson;
    }

    public int getCarsPerPerson() {
        return carsPerPerson;
    }

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

Upvotes: 2

Views: 102

Answers (2)

paulscode
paulscode

Reputation: 1069

Three problems that I can see.

First, you are never instantiating sc (you set it to null, then do nothing with it).

Second, you are setting a double to an Object. (no longer valid after question update)

Third, you are comparing an object with a double.

Most likely you want to compare properties of the object. Maybe something like:

if( arr2[i].getTotalCO2() > highestindex.getTotalCO2() ) {
    highestindex = arr2[i];
}

--EDIT-- Also, change the highestindex line to:

CO2Data highestindex = arr2[0];

Upvotes: 1

hiimjames
hiimjames

Reputation: 518

Im sure your code is not working code.

For example how can you do this:

double highestindex = arr2[0];

Or what is the point of your Scanner class in your methoD? It will not work.

So it is hard to help you if you put here so strange code. Try something this:

public static double highest (CO2Data []  arr2){
    Scanner sc = new Scanner(System.in);
    CO2Data highestindex = arr2[0];
    for (int i = 0; i<arr2.length; i++){
    arr2[i].setTotalCO2(sc.nextDouble());
        if (arr2[i].getTotalCO2() > highestindex.getTotalCO2()){
            highestindex = arr2[i];
            }
        }
    System.out.println(highestindex.getTotalCO2());
    return highestindex.getTotalCO2();
    }

Upvotes: 0

Related Questions