Array list return a double with the highest integer












0















I have a class DrugUseYear that contains a calculation method for the mean drug use used per month. The months are in an Enum class.



An trying to find the highest value within the means found.
I understand why my code is wrong but i am unsure on how to fix it.



public class Rainfall {
private DrugUseYear DrugUseYears;

public double calculateHighestMeanAnnualDrugUse(){
Arrays.sort(DrugUseYears);
double maxValue = DrugUseYears[DrugUseYears.length - 1];
return maxValue;
}
}









share|improve this question

























  • The first thing to do is to read and try to understand the error message from the compiler. It's outputted for a reason: in order for you to read it. You can't initialize a variable of type double with an element of an array of DrugUseYear, because a DrugUseYear is not a double. It's a DrugUseYear.

    – JB Nizet
    Nov 14 '18 at 18:50













  • I would loop through the years and find the highest value. BTW a DrugUseYears is not a double. most likely you need to to get a field like getMeanUse

    – Peter Lawrey
    Nov 14 '18 at 18:50











  • Follow proper naming conventions and keep your variable names lowercase: private DrugUseYear drugUseYears; Also, Arrays.sort() may struggle to sort classes without comparators, so you will likely not be sorting this array as you expect.

    – Bucket
    Nov 14 '18 at 18:50
















0















I have a class DrugUseYear that contains a calculation method for the mean drug use used per month. The months are in an Enum class.



An trying to find the highest value within the means found.
I understand why my code is wrong but i am unsure on how to fix it.



public class Rainfall {
private DrugUseYear DrugUseYears;

public double calculateHighestMeanAnnualDrugUse(){
Arrays.sort(DrugUseYears);
double maxValue = DrugUseYears[DrugUseYears.length - 1];
return maxValue;
}
}









share|improve this question

























  • The first thing to do is to read and try to understand the error message from the compiler. It's outputted for a reason: in order for you to read it. You can't initialize a variable of type double with an element of an array of DrugUseYear, because a DrugUseYear is not a double. It's a DrugUseYear.

    – JB Nizet
    Nov 14 '18 at 18:50













  • I would loop through the years and find the highest value. BTW a DrugUseYears is not a double. most likely you need to to get a field like getMeanUse

    – Peter Lawrey
    Nov 14 '18 at 18:50











  • Follow proper naming conventions and keep your variable names lowercase: private DrugUseYear drugUseYears; Also, Arrays.sort() may struggle to sort classes without comparators, so you will likely not be sorting this array as you expect.

    – Bucket
    Nov 14 '18 at 18:50














0












0








0








I have a class DrugUseYear that contains a calculation method for the mean drug use used per month. The months are in an Enum class.



An trying to find the highest value within the means found.
I understand why my code is wrong but i am unsure on how to fix it.



public class Rainfall {
private DrugUseYear DrugUseYears;

public double calculateHighestMeanAnnualDrugUse(){
Arrays.sort(DrugUseYears);
double maxValue = DrugUseYears[DrugUseYears.length - 1];
return maxValue;
}
}









share|improve this question
















I have a class DrugUseYear that contains a calculation method for the mean drug use used per month. The months are in an Enum class.



An trying to find the highest value within the means found.
I understand why my code is wrong but i am unsure on how to fix it.



public class Rainfall {
private DrugUseYear DrugUseYears;

public double calculateHighestMeanAnnualDrugUse(){
Arrays.sort(DrugUseYears);
double maxValue = DrugUseYears[DrugUseYears.length - 1];
return maxValue;
}
}






java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 18:48









zengr

26.2k31110178




26.2k31110178










asked Nov 14 '18 at 18:48









Klyde MoradeyoKlyde Moradeyo

1




1













  • The first thing to do is to read and try to understand the error message from the compiler. It's outputted for a reason: in order for you to read it. You can't initialize a variable of type double with an element of an array of DrugUseYear, because a DrugUseYear is not a double. It's a DrugUseYear.

    – JB Nizet
    Nov 14 '18 at 18:50













  • I would loop through the years and find the highest value. BTW a DrugUseYears is not a double. most likely you need to to get a field like getMeanUse

    – Peter Lawrey
    Nov 14 '18 at 18:50











  • Follow proper naming conventions and keep your variable names lowercase: private DrugUseYear drugUseYears; Also, Arrays.sort() may struggle to sort classes without comparators, so you will likely not be sorting this array as you expect.

    – Bucket
    Nov 14 '18 at 18:50



















  • The first thing to do is to read and try to understand the error message from the compiler. It's outputted for a reason: in order for you to read it. You can't initialize a variable of type double with an element of an array of DrugUseYear, because a DrugUseYear is not a double. It's a DrugUseYear.

    – JB Nizet
    Nov 14 '18 at 18:50













  • I would loop through the years and find the highest value. BTW a DrugUseYears is not a double. most likely you need to to get a field like getMeanUse

    – Peter Lawrey
    Nov 14 '18 at 18:50











  • Follow proper naming conventions and keep your variable names lowercase: private DrugUseYear drugUseYears; Also, Arrays.sort() may struggle to sort classes without comparators, so you will likely not be sorting this array as you expect.

    – Bucket
    Nov 14 '18 at 18:50

















The first thing to do is to read and try to understand the error message from the compiler. It's outputted for a reason: in order for you to read it. You can't initialize a variable of type double with an element of an array of DrugUseYear, because a DrugUseYear is not a double. It's a DrugUseYear.

– JB Nizet
Nov 14 '18 at 18:50







The first thing to do is to read and try to understand the error message from the compiler. It's outputted for a reason: in order for you to read it. You can't initialize a variable of type double with an element of an array of DrugUseYear, because a DrugUseYear is not a double. It's a DrugUseYear.

– JB Nizet
Nov 14 '18 at 18:50















I would loop through the years and find the highest value. BTW a DrugUseYears is not a double. most likely you need to to get a field like getMeanUse

– Peter Lawrey
Nov 14 '18 at 18:50





I would loop through the years and find the highest value. BTW a DrugUseYears is not a double. most likely you need to to get a field like getMeanUse

– Peter Lawrey
Nov 14 '18 at 18:50













Follow proper naming conventions and keep your variable names lowercase: private DrugUseYear drugUseYears; Also, Arrays.sort() may struggle to sort classes without comparators, so you will likely not be sorting this array as you expect.

– Bucket
Nov 14 '18 at 18:50





Follow proper naming conventions and keep your variable names lowercase: private DrugUseYear drugUseYears; Also, Arrays.sort() may struggle to sort classes without comparators, so you will likely not be sorting this array as you expect.

– Bucket
Nov 14 '18 at 18:50












1 Answer
1






active

oldest

votes


















0














This is incorrect:



double maxValue = DrugUseYears[DrugUseYears.length - 1];


because accessing DrugUseYears array will return an object of type DrugUseYear. You need something like:



double maxValue = DrugUseYears[DrugUseYears.length - 1].getValueToSortBy();


But also this is probably incorrect:



Arrays.sort(DrugUseYears);


To sort object of class DrugUseYear, you need to either implement Comparable interface for that class (which you did not mention doing) or provide a comparator to sort() method:



Arrays.sort(array, new Comparator<DrugUseYear>() {
@Override
public int compare(DrugUseYear o1, DrugUseYear o2) {
return Double.compare(o1.getValueToSortBy(), o2.getValueToSortBy());
}
});





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306903%2farray-list-return-a-double-with-the-highest-integer%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    This is incorrect:



    double maxValue = DrugUseYears[DrugUseYears.length - 1];


    because accessing DrugUseYears array will return an object of type DrugUseYear. You need something like:



    double maxValue = DrugUseYears[DrugUseYears.length - 1].getValueToSortBy();


    But also this is probably incorrect:



    Arrays.sort(DrugUseYears);


    To sort object of class DrugUseYear, you need to either implement Comparable interface for that class (which you did not mention doing) or provide a comparator to sort() method:



    Arrays.sort(array, new Comparator<DrugUseYear>() {
    @Override
    public int compare(DrugUseYear o1, DrugUseYear o2) {
    return Double.compare(o1.getValueToSortBy(), o2.getValueToSortBy());
    }
    });





    share|improve this answer




























      0














      This is incorrect:



      double maxValue = DrugUseYears[DrugUseYears.length - 1];


      because accessing DrugUseYears array will return an object of type DrugUseYear. You need something like:



      double maxValue = DrugUseYears[DrugUseYears.length - 1].getValueToSortBy();


      But also this is probably incorrect:



      Arrays.sort(DrugUseYears);


      To sort object of class DrugUseYear, you need to either implement Comparable interface for that class (which you did not mention doing) or provide a comparator to sort() method:



      Arrays.sort(array, new Comparator<DrugUseYear>() {
      @Override
      public int compare(DrugUseYear o1, DrugUseYear o2) {
      return Double.compare(o1.getValueToSortBy(), o2.getValueToSortBy());
      }
      });





      share|improve this answer


























        0












        0








        0







        This is incorrect:



        double maxValue = DrugUseYears[DrugUseYears.length - 1];


        because accessing DrugUseYears array will return an object of type DrugUseYear. You need something like:



        double maxValue = DrugUseYears[DrugUseYears.length - 1].getValueToSortBy();


        But also this is probably incorrect:



        Arrays.sort(DrugUseYears);


        To sort object of class DrugUseYear, you need to either implement Comparable interface for that class (which you did not mention doing) or provide a comparator to sort() method:



        Arrays.sort(array, new Comparator<DrugUseYear>() {
        @Override
        public int compare(DrugUseYear o1, DrugUseYear o2) {
        return Double.compare(o1.getValueToSortBy(), o2.getValueToSortBy());
        }
        });





        share|improve this answer













        This is incorrect:



        double maxValue = DrugUseYears[DrugUseYears.length - 1];


        because accessing DrugUseYears array will return an object of type DrugUseYear. You need something like:



        double maxValue = DrugUseYears[DrugUseYears.length - 1].getValueToSortBy();


        But also this is probably incorrect:



        Arrays.sort(DrugUseYears);


        To sort object of class DrugUseYear, you need to either implement Comparable interface for that class (which you did not mention doing) or provide a comparator to sort() method:



        Arrays.sort(array, new Comparator<DrugUseYear>() {
        @Override
        public int compare(DrugUseYear o1, DrugUseYear o2) {
        return Double.compare(o1.getValueToSortBy(), o2.getValueToSortBy());
        }
        });






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 19:07









        syntagmasyntagma

        12.8k1249106




        12.8k1249106
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306903%2farray-list-return-a-double-with-the-highest-integer%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python