Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9296311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T21:48:15+00:00 2026-06-18T21:48:15+00:00

I have created a method which is supposed to return type double and here

  • 0

I have created a method which is supposed to return type double and here is my code

private void myMethod()  
{ 
    if(myArrayList.size() >= 2) 
    {
        Double  t = myArrayList.get(myArrayList.size()-1);
        Double  d = myArrayList.get(myArrayList.size()-2); 
        Double result = ( t+ d  ) / 2 ;
        System.out.println("Average is: "+result);
    }
}

I change void to double and just after system.out….. line i added return result but this gives an error!! Could you please tell me how exactly i can change this method so that i obtain a return type of double?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T21:48:16+00:00Added an answer on June 18, 2026 at 9:48 pm

    To sum things up, all the previous answer are right, but lack some minor detail, like @dinesh707 says, you must be able to return on every method branch (unless an exception occurs). And also be sure to declare the result variable outside the decision/if block, and to declare that the method return a double, and you should go with the double primitive, not the boxed version.

    so the code looks like:

    private double myMethod() { 
    double result=0; 
    if(myArrayList.size() >= 2) { 
        double t = myArrayList.get(myArrayList.size()-1); 
        double d = myArrayList.get(myArrayList.size()-2); 
        result = ( t+ d ) / 2 ; 
        System.out.println("Average is: "+result); 
    } 
    return result; 
    

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following method which is supposed to return a JSONResult so I
I have created a Webpage which will post as post method..not as get method.
I have created a custom shipping method to show depo-names from which customers will
I have created a class library called AddServiceLibrary in which I have a method
I have created a class which extends Gallery. There is no onCreate() method in
We have repositories which have a Save method. They also throw a Created event
I have a web service method in which I create a particular type of
I have an Extension method which is supposed to filter a Queryable object (IQueryable)
I have a winforms, and it connecting wit webservice. Webservice has method which create
I'm developing an ASP.NET forms webapplication using C#. I have a method which creates

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.