• Post Reply Bookmark Topic Watch Topic
  • New Topic

Return Statement with in if condition  RSS feed

 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am writing a program, where, From the main program, a method will be called. Method will accept two parameters.
In the method, i need to check for the null of two parameters which are passed to the method. IF it is not null, then,
do some calculations and send it to main program as a Dobule value.
My question is: I am not able to put return statement with in IF condition. at the same time, I am not able to declare the Double value outside IF conditon.
Please help.


 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On line 10 c is not declared. It is only in scope within the conditional block.

And of course you can put a return in a conditional block. I suspect the error you got was because you failed to account for what would be returned if the condition evaluated to false.
 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.

on Line no. 10, I have declared c and assigning the value to it. But, how do i access that variable from outside IF condition?

I feel that Else part is not requried in my program.

please Help
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot access a variable outside of its defined scope. If you find that you need to, it's a clear sign that your code is poorly designed and needs to be refactored.

I didn't say anything about else. I said that no matter what happens, the method must return a value. You have declared that the method returns a Double, and it must always do so. Even if that value is null.
 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault, Thank you for the reply.

can you please suggest me, how i can fix this issue? I need to return a Double value. That Double value is calculated with in IF condition.
Please help.
 
Riaan Nel
Ranch Hand
Posts: 160
IntelliJ IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anu satya wrote:
I feel that Else part is not requried in my program.

Why not?

Alternatively, instead of returning null or some other 'dummy' value, you could just throw an IllegalArgumentException or a NullPointerException. Either way, using an else block is perfectly sensible. If you really don't want to use an else, you could return from within your if statement, return null at the end of the method. If method returns from the calling method in the if block, the end of the method will never be reached. If the if check fails, you can assume that the return at the end of the method will be returned.
 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI Nel,
Thank you for the reply.

In my program, I don't want to do anything in ELSE part.
So, How do i handle this "return Double" issue? where should i put this return statement?

Please help!
 
Akhilesh Trivedi
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anu satya wrote:

In my program, I don't want to do anything in ELSE part.



"Else" part of an if-statement is always optional, you can chose to simply not writing it.
You can also have an else block with just a semi-colon " ; " inside.
 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Thanks for the inputs.

Can any one please help me with return statement?
Please??
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You still haven't said what you want to happen in the case that the null-check fails. We can't decide that for you. The method says it returns a value, so you need to return a value or throw an exception.
 
Kumaravadivel Subramani
Ranch Hand
Posts: 171
Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Try this code, I guess this is what you wanted to have. But it create problem when the exception occurs as already said in the previous post.


private Double calculate(Double a, BigDecimal b) {
Double c = null;
if(!((null == a)&&(null == b))){

//do some calculation
c = Double.valueOf(b.doubleValue());
}
return c;
}

Declare and initialize before the block of "if" and assign the value if needed, then return the value.
 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:You still haven't said what you want to happen in the case that the null-check fails. We can't decide that for you. The method says it returns a value, so you need to return a value or throw an exception.

Thanks for the reply.
I neither want to throw any exception nor return null value in Else part. (That is the requirement)

My only concern is: how to return c.

pleae help me with that.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anu satya wrote:I neither want to throw any exception nor return null value in Else part. (That is the requirement).

That is not possible. The compiler insists that the method must return a value or throw an exception in all circumstances. Otherwise you can have code that calls the method with null a and b - what happens then? You must return something. Not knowing what to return is the cause of the problems you're having. To return what you want all you needs is a return b.doubleValue() anywhere you want - it still won't compile if there are any paths that don't return a value. You say "I neither want to throw any exception nor return null value" - but what do you want to do in that case?

Kumaravadivel's shown you how to return c - but in that case it will return null when the check fails. Which is sensible, and may be what you need, but clashes with what you say you need.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anu satya wrote:I neither want to throw any exception nor return null value in Else part. (That is the requirement)

Then I suggest you use some language other than java.

You declare that your method will return a Double:

private Double calculate...

That means that no matter what path is executed, you will ALWAYS get to a return statement that indeed returns a double (or a NULL, which can fill in for anything).

The language was defined to work that way. There simply is no way to create a method that sometimes returns a value and sometimes doesn't. It's all or nothing.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:
Anu satya wrote:I neither want to throw any exception nor return null value in Else part. (That is the requirement)

Then I suggest you use some language other than java.

Mmmm, I disagree a bit with the last two responses. If the requirement is to not throw an exception or return null, there is still another option within the rules of Java: you can return a Double object, provided you can find a value that is suitable. And if you look at the API for Double, there actually is a value that you can return that might be appropriate. (It's defined there as a double rather than a Double, but you can let autoboxing take care of that.)

Now if you just don't want to return something in an else statement, or in a statement after the if statement, tough. If the method returns a Double, then you have to either return a Double, return a null, or throw an exception. Period. But the way it's been phrased above, I think there way still be a solution to your problem, by returning a different value of Double. I hope that helps...
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:Mmmm, I disagree a bit with the last two responses. If the requirement is to not throw an exception or return null, there is still another option within the rules of Java

That's not quite what I was getting at though. It's got to return something, and Atu's got to decide what that something is, which is what we've been trying to explain. Just listing things it can't return is not a help.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I think we agree on all that. I only disagreed with where both you and Fred quoted a specific line and said that can't be done. It can be done, even if we accept those requirements Anu listed in the part you and Fred quoted. We agree that Anu still needs to return something, and needs to decide what he does want to return in the even the if condition is not true. I was just hinting at an option that may not have been considered yet.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!