0

I have written such a simple code:

  • I have written this line in my class's constructor : List element = new ArrayList();

  • I have a variable named cost which its type is int

  • one method will return three lists with different objects: listOne, listTwo, listThree

  • in another method I have written below code which this method will use those lists that are created in the method above.this method will be called three times for three lists above. each call for each list.

    // begining of the method:
    int cost = 0;
    
    if(cost==0){
        element = listOne;
        cost = 3;
    }
    if(cost<4){
        element = listtwo;
        cost = 6;
    }
    // end
    
    System.out.println(element.toString());
    

Unfortunately, instead of printing listTwo it will print listThree (if we have 4 or more lists it will print the last one)!

Is there any problem with if-else condition?

thanks

EDIT: this is my main code but its condition is like the code above: auxiliaryList in the code below is listOne or list Two or listThree respectively.

cost = 0;

public void method {


                System.out.println(element.toString());//number one
                if (cost== 0) {


                element = auxiliaryList;
                cost = 3;

                }
                else if( cost<4){

                        element =auxiliaryList;
                         cost = 6;
                }

            }
            return;

        }

}

also the line which is declared with //number one shows me that before going to the if/else condition ,the element list will be set to the current list in the method.

4
  • I don't see a if-else condition, only two if Commented Mar 1, 2011 at 7:15
  • seems like its printing last list only. can you debug the value of cost. One more check could be the lists might be getting empty because of initializing the constructor. else move the initialization List element = new ArrayList(); outside of constructor Commented Mar 1, 2011 at 7:17
  • the value of cost is Ok!! but as I said for Jon Skeet:I have checked that before going in to the if-else condition ,the element list will be set with the current list.I mean for the third list,before going in to the if-else condition,the element list will be set to thirdList!!! Commented Mar 1, 2011 at 7:27
  • 1
    You say that "This is my main code", but it doesn't even compile, as you posted it! Why don't you post the real code? Posting a SSCCE would be even better. Commented Mar 1, 2011 at 7:52

1 Answer 1

5

Is there any problem with if-else condition.

Yes - the problem is that you're not using if/else, you're just using two if statements.

Change the second if to use an else and it'll be fine:

  if (cost == 0) {
      element = listOne;
      cost = 3;
  } else if (cost < 4) {
      element = listtwo;
      cost = 6;
  }

The problem was that if cost was 0, it would enter the first block, set cost to 3, and then go into the second block because 3 is less than 4. There was no "else" to stop that happening - the two if blocks were entirely independent.

Sign up to request clarification or add additional context in comments.

4 Comments

@user472221: In that case you need to give us a short but complete program which demonstrates the problem. I've fixed what's wrong with the small portion of the program which you've shown us, but there could be other things wrong with it in any number of places.
Ok,also I have checked that before going in to the if-else condition ,the element list will be set with the current list.I mean for the third list,before going in to the if-else condition,the element list will be set to thirdList!!!
@user472221: That's still not a short but complete program. Please post something we can compile, paste, compile and run with no other changes.
@user472221: Create a copy of it, and cut down everything that isn't required just to demonstrate the problem. In doing so, you're very likely to find the problem yourself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.