sys_debug
sys_debug

Reputation: 4003

a simple int comparison causing issue

I've got the following code:

 int bookingType=0;
        //mapSize=mapLoaded.size();

        DateTime startx = new DateTime(startDate.getTime());
        DateTime endx = new DateTime(endDate.getTime());

        //booking status
        boolean possible = false;

        //Booking type: 1 = Project, 2 = Training

        if(requestType.equals("Project")){
            bookingType = 1;
        }else if(requestType.equals("Training")){
            bookingType = 2;

        }

        //produces submap
        //mapSize = bookingType;

        bookingType = 1;
        if(bookingType==1)
        {
            .....
        }else if(bookingType==2){
           ....
       }

The error after testing is when am checking value of bookingType. usually the == would work but here it seems to be skipping it altogether. I checked the value of bookingType and it does change to 1 or 2 based on requestType. Why is the if() not working?

Thanks,

Upvotes: 0

Views: 99

Answers (2)

Shantha Kumara
Shantha Kumara

Reputation: 3421

According to your code it should go to first if condition.

Can you please put the else condition and print the value in bookingType ?

And also make sure that you do not set bookingType in any other place. It will be more helpful if you could provide more complete code sample. And provide how do you determine the if condition check is not working and error.

Upvotes: 1

LeleDumbo
LeleDumbo

Reputation: 9340

here you set bookingType:

if(requestType.equals("Project")){
    bookingType = 1;
}else if(requestType.equals("Training")){
    bookingType = 2;
}

here you override it:

bookingType = 1;
if(bookingType==1)
{
    .....
}else if(bookingType==2){
    ....
}

of course the if would only execute the if part. Since you post incomplete code, you can try adding else clause to the if-else if chain and see whether it gets executed or not.

Upvotes: 1

Related Questions