Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Missing Return Statement

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My code is below... when I compile, it gives me a "Missing Return Statement" on my middle method. I realize that it's due to it being Double instead of Static... I don't fully understand Method openings and their impact. This is like my first week of playing with Java. :-/

I am bolding the method that is throwing the compilation error.

Thanks!!

[edit]Add code tags. CR[/edit]
 
Marshal
Posts: 79630
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch

As you remember, a method heading has several parts (you can find full details in the Java Language Specification but it's not easy to read):
  • Access modifier (optional): private, public or protected.
  • Other modifiers (optional), eg static final, synchronized.
  • Generic formal type parameter (optional) eg <T>, and I think that comes here rather than later.
  • return type (compulsory). If the method returns nothing, write void instead of the return type.
  • method identifier (compulsory)
  • (
  • List of parameters, separated by commas. A zero-length list is permitted
  • )
  • In the example you quoted, the method declares a return type of "double." This tells the compiler that a double value will come back when that method completes. You should be able to write

    double sum = getSum(numbers);

    So the compiler goes through every method and looks whether it returns what it claims to, from every possible "path" through the method.
    Your method claims to return a double, but doesn't return a double. It actually behaves as a method which doesn't return anything (see above). It also has the peculiarity that it claims to take a double[] as a parameter, but it doesn't do anything with that parameter. It calculates and prints the total of an int[]. So you have an unused parameter; that ought to be deleted.
     
    Campbell Ritchie
    Marshal
    Posts: 79630
    380
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Please use the code button; I have edited your post so you can see how much better it looks.
    You can find the Java Language Specification section here, but it is by no means easy to read.
     
    C Hurd
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you so very much!

    Be warned that I will be back with many many questions .
     
    Campbell Ritchie
    Marshal
    Posts: 79630
    380
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You're welcome
    reply
      Bookmark Topic Watch Topic
    • New Topic