Is there a way in java to print the answer of a boolean (true/false) in just one printline? So not via an if-statement etc. If so what would the print statement look like?
boolean flag = true;
System.out.println(flag != false?true:false);
System.out.println(booleanResult);
Related
Here i want to combine the below 2 expectations into one.
expect(button.getText()).toEqual('Process Successful');
expect(button.getText().indexOf('- code 3001')).toBeGreaterThan(0);
Also whether the below statement is correct or not. I am trying to verify in the getText() whether expected value is present in the text.
expect(button.getText().indexOf('- code 3001')).toBeGreaterThan(0);
You can use expect().toContain() to verify text contained in string.
expect(button.getText()).toContain('Process Successful');
expect(button.getText()).toContain('- code 3001');
You can also do it in another way,
var buttonContainsText = button.getText().then(function(text){
return (text.indexOf('Process Successful') > -1) && (text.indexOf('- code 3001') > -1)
})
expect(buttonContainsText).toBeTruthy();
I'm just starting out with perl. I want to set a boolean variable flag based on if the hash map has content or not. This tells me I can use a ! operator to check if hash empty. how to check if a hash is empty in perl
So I have this so far:
if (!%someHash){
$flag = false;
} else {
$flag = true;
}
Is this a best way of writing it or there is a simpler way?
Since perl doesn't have boolean types I've always just done:
my $flag = keys %someHash
I'm trying to see if its possible to test for a certain result specific to a boolean value in an SQL database.
if($result = $mysqli->query("SELECT * FROM `work_orders` WHERE `is_open` = '1' "))
{
show_workorder($result);
}
I would then want to execute:
#TRUE allows this to show
if($result = True){
echo "<p> No current work orders are open. Good Work !</p>\n";
return;
}
If i change database in work_orders.is_open , the boolean is 0, meaning its "not open" .. the if statement for TRUE above, still shows...
I am sure that I'm missing something.
Seems I have solved my own question:
the if statement should just test for rows that were returned or not.
if($result->num_rows == 0)
this would allow it to show if there is nothing returned. If results are returned then it will continue moving forward.
I find myself often writing code like this:
if ($optionalParamsRef->{verbosity}) {
$settingsHash{verbosity} = $optionalParamsRef->{verbosity};
}
However, it seems very verbose to repeat $optionalParamsRef->{verbosity} twice. Is there a shorter way?
Edit: Yes, I realize this is checking for true/false and not 'exists'. What I'm looking for is a concise functional equivalent to this.
Note you are checking $optionalParamsRef->{verbosity} for true, not exist.
Possible way to do this:
foreach my $k (qw/verbosity param1 param2 param3/) { #Enumerate keys here
$settingsHash{$k} = $optionalParamsRef->{$k} if exists($optionalParamsRef->{$k});
}
As others mentioned, your code checks for false-ness. If you considered false values as non-existant, you could have used the logical or. Probably this is not what you want.
$settingsHash{verbosity} = $optionalParamsRef->{verbosity} || $default;
But maybe defined-ness is enough. It's still no check for existence, but if your hash doesn't contain undef values, this could be enough:
$settingsHash{verbosity} = $optionalParamsRef->{verbosity} // $default;
using the "new" defined-or operator // instead of the logical or ||. I know these examples are not equivalent to the code you posted because they alwas assign something, but in my experience, this is often useful, so maybe it could help.
my $v = $optionalParamsRef->{verbosity};
$settingsHash{verbosity} = $v if $v;
for ($optionalParamsRef->{verbosity}) {
$settingsHash{verbosity} = $_ if $_;
}
A concise functional equivalent:
sub {$_[0]=$_[1] if $_[1]}->($settingsHash{verbosity}, $optionalParamsRef->{verbosity});
However, IMO, the main problem with your code is that you are only conditionally setting $settingsHash{verbosity}, keeping you from doing something simpler like:
$settingsHash{verbosity} = $optionalParamsRef->{verbosity} || somedefault
or even:
%settingsHash = ( %defaultSettings, %$optionalParamsRef );
I am having a strange problem with boolean logic. I must be doing something daft, but I can't figure it out.
In the below code firstMeasure.isInvisibleArea is true and measureBuffer1 is nil.
Even though test1 is evaluating to NO for some reason it is still dropping into my if statement.
It works ok if I use the commented out line.
Any idea why this happens?
BOOL firstVisible = firstMeasure.isInVisibleArea;
BOOL notFirstVisible = !(firstMeasure.isInVisibleArea);
BOOL measureBufferNil = measureBuffer1 == nil;
BOOL test1 = measureBuffer1 == nil && !firstMeasure.isInVisibleArea;
BOOL test2 = measureBufferNil && !firstVisible;
if (measureBuffer1 == nil && !firstMeasure.isInVisibleArea)
//if (measureBufferNil && !firstVisible)
{
//do some action
}
Update 1:
I isolated the problem to !firstMeasure.isInVisibleArea as I've entirely taken on the measureBuffer bit.
Inside isInVisible area is a small calculation (it doesn't modify anything though), but the calculation is using self.view.frame. I am going take this out of the equation as well and see what happens. My hunch is that self.view.frame is changing between the two calls to isInVisibleArea.
Update 2:
This is indeed the problem. I have added the answer in more detail below
When in doubt, you should fully parenthesize. Without looking up the precedence rules, what I think what is happening is that = is getting higher precedence than == or &&. So try:
BOOL test1 = ((measureBuffer1 == nil) && !firstMeasure.isInVisibleArea);
While you certainly can parenthesize, you should also know that nil objects evaluate to boolean NO and non-nil objects evaluate to boolean YES. So you could just as easily write this:
BOOL firstVisible = firstMeasure.isInVisibleArea;
BOOL notFirstVisible = !(firstMeasure.isInVisibleArea);
BOOL measureBufferNil = measureBuffer1;
BOOL test1 = !measureBuffer1 && !firstMeasure.isInVisibleArea;
BOOL test2 = measureBufferNil && !firstVisible;
if (measureBuffer1 && !firstMeasure.isInVisibleArea) {
//do some action
}
You would end up with the same results. I agree with GoatRider, though. It's always far better to parenthesize your conditional expressions to clarify what you really want to happen than it is to rely on the language's operator precedence to do it for you.
If test1 is evaluating to NO as you say, then drop test1 into the if statement:
if(test1){
//see if this executes?
}
See what that does.
My hunch was correct, it is related to the view frame changing between calls to firstMeasure.isInVisible area.
This whole routine is called in response to the view moving. I think I need to grab the value of firstMeasure.isInVisibleArea at the start of the method and use that value throughout.
Phew. Boolean logic isn't broken. All is right with the world.
Thanks for all your input