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


the array part of class called co2data array co2data array. problem i'm trying find highest value in array method isn't working. values array method must first them co2data class. what's wrong method:

    public static co2data highest (co2data []  arr2){ scanner sc = null; co2data highestindex = arr2[0].gettotalco2; (int = 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 co2data class looks like:

public class co2data {

private string country; //a private variable prevent other users accessng , chaning these variables.  private double totalco2; private double roadco2; private double co2perperson; private int carsperperson;  public co2data() {      country = "";//this sets initial values different variables     totalco2 = 0;     roadco2 = 0;     co2perperson = 0;     carsperperson = 0; }  public string getcountry() {     return country; }  public void setcountry(string country) { //to set country have use command access private variable     this.country = country; //you have use this.country instead of country because string county has been declared private variable.  }  public double gettotalco2() {     return totalco2; }      public void settotalco2(double totalco2) {         this.totalco2 = totalco2; //the this.item command allows 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;     } } 

three problems can see.

first, never instantiating sc (you set null, nothing it).

second, setting double object. (no longer valid after question update)

third, comparing object double.

most want compare properties of object. maybe like:

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

--edit-- also, change highestindex line to:

co2data highestindex = arr2[0]; 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -