• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to create a valid return statement in a method  RSS feed

 
Sucheta Shrivastava
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a method called CalculateSimpleInterest () that calculates the interest according to the season type. Its return type is double . How to make it work correctly. I have a code that is throwing compile time error saying you need a valid return statement.


 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have a series of if-else-if checks but no unconditional else part. Either add plain else clause with a return at the end as a "everything else" or just add a return after the if-else-if statements to return a value when none of the conditions before it are met.
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since your conditions are based on the SeasonTyoe, it's probably better to use a switch-case statement instead.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the compiler is smart enough to know that it is possible for this code to execute and not always hit a return statement. For example. if seasontype was set to FALL, what would the method return? 

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!