my dog learned polymorphism
The moose likes Beginning Java and the fly likes Question re:  nested IF statements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question re:  nested IF statements" Watch "Question re:  nested IF statements" New topic

Question re: nested IF statements

Melinda Savoy
Ranch Hand

Joined: Jun 21, 2005
Posts: 387

I have the following nested if statements whereby I am trying to launch the action code to work in either case. Would someone please let me know what I am doing wrong and how I can make this work without having to duplicate the action code in an else statement because it is pretty long?

Here is my code:

if (readRequest.getTransactionID() != null &&
readRequest.getTransactionID().intValue() == Transaction.get(readRequest.getTransactionID()).GetId()){
if (readRequest.getTransactionID() == null){
Transaction transaction = new Transaction(); readRequest.setTransactionID(BigInteger.valueOf(transaction.GetId()));

Thank you.
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Its a good idea if you are slightly confused about the logic for a conditional statement to write out what you want to do in English before trying to do it with code. If you do you might spot why the code inside your nested conditinal statement will never run. (Hint: look at the null checks)
[ August 24, 2005: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9059
I suggest you introduce some variables to simplify this, at least one for readRequest.getTransactionID().

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
suresh koutam
Ranch Hand

Joined: Dec 29, 2004
Posts: 30
if i understood ur code correctly..then i know where u are doing the mistake..
see ur if conditions....
the first IF will aloow u inside the loop only if the two conditions are met...where the first one is not a null...
so when the transactionId is not a null and the other condition is true it enters the loop...then check the second IF....
u are checking for NULL again....
remember when the transaction ID is not NULL it entered the can it be null inside....
u have to hcange the condition...
U are contradicitng ur IF statments...

SCJP aspirant<br />SCWCD aspirant
Melinda Savoy
Ranch Hand

Joined: Jun 21, 2005
Posts: 387

Hey guys,

Thanks so much for all the help. Marilyn, thanks especially for the link to the additional info it was very helpful.

I agree. Here's the link:
subject: Question re: nested IF statements
It's not a secret anymore!