---
title: "How do I write a method to determine the highest value in an array"  
description: "How do I write a method to determine the highest value in an array"  
author: "Anonymous User"  
published: 2014-12-29  
updated: 2014-12-30  
canonical: https://www.mindstick.com/forum/12826/how-do-i-write-a-method-to-determine-the-highest-value-in-an-array  
category: "java"  
tags: ["java", "array", "class"]  
reading_time: 2 minutes  

---

# How do I write a method to determine the highest value in an array

The [array](https://www.mindstick.com/articles/335/jagged-array-in-c-sharp-dot-net) is part of a [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp) called CO2Data so the array is a CO2Data array. The [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) is I'm [trying](https://answers.mindstick.com/qa/93698/6-mistakes-couples-are-trying-to-save-money) to find the [highest value](https://www.mindstick.com/forum/161550/how-to-get-the-second-highest-value-in-a-column-without-using-a-subquery-with-top-or-order-by) in the array but my [method](https://www.mindstick.com/forum/166/webservice-method) isn't working. To get the [values](https://www.mindstick.com/forum/327/sum-textbox-values) from the array the method [must](https://www.mindstick.com/articles/12978/top-reasons-why-every-magento-ecommerce-store-must-opt-for-mobile-app) first get them from the CO2data class. So what's [wrong](https://answers.mindstick.com/qa/48468/who-wrote-the-the-wrong-enemy-america-in-afghanistan-2001-2014-and-when) 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;    }}
```

## Replies

### Reply by Jayden Bell

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];}Also, change the highestindex line to:\
CO2Data highestindex = arr2[0];


---

Original Source: https://www.mindstick.com/forum/12826/how-do-i-write-a-method-to-determine-the-highest-value-in-an-array

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
