Click to See Complete Forum and Search --> : Problem with boolean


javaQQ
August 31st, 2006, 01:19 PM
Is there anything in this block of code which would set a boolean to "true?"

// --------------------------------------- has the job been cancelled?
if( !cancel_datetime.equals( "11-11-9999" ) ) { // i.e.: the job has been cancelled
cancelled = true;
}
else
if( cancel_datetime.equals( "11-11-9999" ) ) { // i.e.: the job has NOT been cancelled
cancelled = false;
}



This is the context in which the block appears:

String _default_ = "11-11-9999";
...

boolean cancelled = false;
...

// --------------------------------------- has the job been cancelled?
if( !cancelDatetime.equals( "11-11-9999" ) ) { // i.e.: the job has been cancelled
cancelled = true;
}
else
if( cancelDatetime.equals( "11-11-9999" ) ) { // i.e.: the job has NOT been cancelled
cancelled = false;
}
...

if( cancelled = true ) {
System.out.println( "cancelled (1) = " + cancelled );
// Dialog box: the job has been cancelled
showADialog( frame, "cancelled" );
}
...

System.out.println( "cancelled (2) = " + cancelled );


If I run the application with the block commented-out, "cancelled" prints out to "false."
Include the block, "cancelled" prints out to "true."
An exact duplicate of the model works with other variables.


Many thanks in advance for a solution.

Krishnaa
August 31st, 2006, 01:26 PM
Which block did you commented ? and whats the value of cancelDatetime ?

javaQQ
August 31st, 2006, 01:45 PM
Which block did you commented ? and whats the value of cancelDatetime ?

This code, when commented-out leaves the boolean "false." When included, changes the boolean to "true." This happens regardless of the value of "cancelDatetime."

[/code]
// --------------------------------------- has the job been cancelled?
if( !cancelDatetime.equals( "11-11-9999" ) ) { // i.e.: the job has been cancelled
cancelled = true;
}
else
if( cancelDatetime.equals( "11-11-9999" ) ) { // i.e.: the job has NOT been cancelled
cancelled = false;
}
[/code]

Krishnaa
August 31st, 2006, 01:58 PM
Did you tried printing the value of cancelDatetime ?

javaQQ
August 31st, 2006, 02:29 PM
Here is the source of the problem:
"if( cancelled = true ) { ... "
functions the same as
"cancelled = true"

When I change to:
"if( cancelled ) { ... "
The method works.

Question: Is this how it is SUPPOSED to work, or is this a quirk of my platform;
Java 1.4.x, on Mac OS X 1.4.x?

keang
August 31st, 2006, 04:39 PM
Here is the source of the problem:
"if( cancelled = true ) { ... "Yes you're quite right. This line code sets 'cancelled' to true rather than comparing it with true. You have to use '==' for comparisons.

Krishnaa
August 31st, 2006, 04:50 PM
Here is the source of the problem:
"if( cancelled = true ) { ... "
functions the same as
"cancelled = true"

When I change to:
"if( cancelled ) { ... "
The method works.

Question: Is this how it is SUPPOSED to work, or is this a quirk of my platform;
Java 1.4.x, on Mac OS X 1.4.x?

Now you posted the problem, all earlier posts were junk. :D

As Keang has already posted the answer, I would just say that don't forget to post all the information which you have in first post itself.

Good Luck ! :wave:

keang
August 31st, 2006, 05:59 PM
Now you posted the problemApologies for jumping in with the answer after you'd done all the hard work finding out what the problem was, I hadn't read the rest of the posts in this thread. But now that I have...

if( !cancel_datetime.equals( "11-11-9999" ) ) { // i.e.: the job has been cancelled
cancelled = true;
}
else
if( cancel_datetime.equals( "11-11-9999" ) ) { // i.e.: the job has NOT been cancelled
cancelled = false;
}

JavaQQ why are you you evaluating the same expression twice?
If the first 'if' clause is true then the 'else if' clause never executes and if it is false then the else if clause will always be true (assuming you haven't got a threading problem).

So you could use:
if( !cancel_datetime.equals( "11-11-9999" ) ) { // i.e.: the job has been cancelled
cancelled = true;
}
else if { // i.e.: the job has NOT been cancelled
cancelled = false;
}Or better still:
cancelled = !cancel_datetime.equals( "11-11-9999" ) ;

Krishnaa
August 31st, 2006, 06:08 PM
Apologies for jumping in with the answer after you'd done all the hard work finding out what the problem was, I hadn't read the rest of the posts in this thread. But now that I have...



No worries, it's not like my department. ;)

dlorde
August 31st, 2006, 07:02 PM
This code, when commented-out leaves the boolean "false." When included, changes the boolean to "true." This happens regardless of the value of "cancelDatetime." ...Are you sure? Even if cancelDatetime is a String with the value "11-11-9999" ?

Time is an excellent teacher; but eventually it kills all its students...
Anon.