• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

missing return value error

 
Greenhorn
Posts: 8
Python Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I am trying to return the value SavingsAcc in this coding exercise. I want to do this so that I can put together multiple methods in a single line. What do I need to do to fix it?

Here is the code;


here is the error:



SavingsAcc2.java:29: missing return value
return;
^
SavingsAcc2.java:35: missing return value
return;
^
SavingsAcc2.java:41: missing return value
return;
^
3 errors


I am doing all of this from the command line I plan on using eclipse or DR java soon but I just don't know them yet.

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The method declarations such as "public SavingsAcc2 addInterest(double interest)" indicate a type of "SavingsAcc2" is expected to be returned. The return statement is not returning anything.

Not sure what the intent is. If you do not want anything returned, you can delcare void as the return type "public void addInterest(double interest)". Otherwise, you'll need to return an instance of SavingsAcc2 as it is currently coded.
 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason for the error is because your methods:



specify a return type "SavingsAcc2" but don't return a value of that type. In fact, they don't return a value at all of any type. Either change the return type to void, meaning you won't be returning anything, or provide a value of type SavingsAcc2 in your return statement.

Other pointers
- indent your code properly.
- a method will return when it is done, a "return;" is not required.
- I don't know what you mean by "so that I can put together multiple methods in a single line." Can you show an example?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Greg Brannon wrote:- I don't know what you mean by "so that I can put together multiple methods in a single line." Can you show an example?


I believe he's talking about "method chaining".

If the methods all return this, you can do things like:
It's really common in JavaScript, but not so much in Java APIs for some reason.

I frequently wish that the JavaBean standard had specified that setters return this. It'd make code so much terser.

If that's the intent, then all those return statements should be:
 
Brian Burress
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bear,

Thanks for the comments. I did not understand what what was envisioned either. Returning 'this' would solve the compile errors. While this would be 'legal' in a java sense, I question the value of coding methods to operate this way as you are limiting yourself to be able to do it only with methods which do not return anything.

If there is a need for the user to perform many method calls at once, perhaps a helper method which invokes them all would be a better approach. I digress from the main question a little, sorry!!
 
Daniel Brackett
Greenhorn
Posts: 8
Python Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Bear Bibeault! Success! and yes I was talking about method chaining!
 
Once upon a time there were three bears. And they were visted by a golden haired tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic