Check if RadioButton is NOT selected - java

I have this logic in my code
if(r40.isChecked()){
//do this
}
It works great, but I needed the negative, "if rb.isNotChecked" or ".isChecked() = false"
But I cant see to find it, also I can't use the RadioGroup because all my RadioButtons are not inside one, I'm controlling them manually

But I cant see to find it
if(!r40.isChecked()){
//do this
}

I mean you COULD do if(r40.isChecked() == false){
//do something
}
BUT if(!r40.isChecked){
//do something
} Is much better/cleaner

Try with negative check
if(!r40.isChecked()){
//do this
}
this check if radio isn`t checked.

Try this:
if (! r40.isChecked()) {
//Do something
}
In more detail the exclamation mark added in this code reverses the following code in the same block, causing the code to mean:
if (r40.isNotChecked()) {
//Do something
}
I hope this helped!

Related

else if two lines instead of one line

I just need to know that if you do an else if on two lines does it still perform the same as an else if on one line?
a bunch of ifs then.....
else if(somecondition here) {
//do some stuff here
}
does the below code perform the same as above?
else
if(somecondition here) {
//do some stuff here
}
OR does it perform as an ELSE by itself then goes into the if after all else, like a normal else statement would.
Whitespace doesn't matter in Java, as long as you have at least one. So the two snippets are equivalent.
Even if you write
else if (...)
or
else
if (...)
it will still be the same.
OR does it perform as an ELSE by itself then goes into the if after all else, like a normal else statement would.
No, here the if statement is technically inside the else clause. If you want the above behaviour, try putting a ; after else. This makes the if not part of the else clause.
if (...) {
} else; // note the semicolon
if (...) {
}
But you'll never write that anyway, you'll just remove the else altogether.
else if(somecondition here) {
//do some stuff here
}
and
else
if(somecondition here) {
//do some stuff here
}
both are same. Java doesn't use newline to determine new statement
If you want the else to be itself and if to be in it you should change it to-
else {
if(somecondition here) {
//do some stuff here
}
}
Technically it is same as else if in some cases

Should I avoid using "!" in if statement in Java?

Our team's Java Coding Guideline says:
Avoid using "!" in if statement as much as possible.
I have asked other colleagues, but no one gave me clear ideas why, because the guideline was created a long time ago and the author might have left our company.
Do you have any idea?
With the information provided, this calls for some speculation. One possible reason is that the intent was not for an if-statement by itself but for an if-else statement. In that case, I can see where you might say that you should reverse the cases so that you don't have the extra operation of the negation. Instead of
if (! boolVar) {
// Something
} else {
// Something else
}
you might prefer
if (boolVar) {
// Something else
} else {
// Something
}
Whether this is worth it or not is probably more a matter of taste and standardization than anything else.
The rule is likely an adaptation from Robert Martin's Clean Code, page 302:
Negatives are just a bit harder to understand than positives. So, when possible, conditionals should be expressed as positives. For example:
if(buffer.shouldCompact())
is preferable to
if(!buffer.shouldNotCompact())
As an example, suppose you're creating a validator that requires two things to be false for the entity to be valid:
The entity must not have been created within the last 12 hours, and
The entity's bank account total sum must not exceed $50,000.
Naturally the idea would be to write two methods for this:
boolean isCreatedWithinLastTwelveHours(BankAccount account)
boolean hasMoreThanTotalSumCap(BankAccount account)
...at which point, you then invoke these as:
boolean newAccount = isCreatedWithinTheLastTwelveHours(account);
boolean highEndAccount = hasMoreThanTotalSumCap(account);
if(!newAccount && !highEndAccount) { // ... other logic
// The more astute would use DeMorgan's law in an effort to make this more readable
if(!(newAccount || highEndAccount)) { // other logic
Well...wouldn't it be nicer if you just said what they weren't instead?
boolean isNotCreatedWithinLastTwelveHours(BankAccount account)
boolean hasLessThanTotalSumCap(BankAccount account)
That'd make the expression a bit more concise:
if(notNewAccount && notHighEndAccount) { // .. carry on!
Of course "!" can be used when you like. There is no "unless" in java and you have no other choices in some conditions.
Looks like yet-another-useless-rule. Generally speaking, there are no absolute terms in this scenario, true that if you are in a if-else clause then possibly it is better to write
if(myCondition) {
doThis()
} else {
doSomethingElse()
}
Instead of
if(!myCondition) {
doSomethingElse()
} else {
doThis()
}
However, that said, in some scenarios is actually quite ok to use the negation operator, particularly if no else clause is provided, example
if (!tokenDoesCompute()) {
throw InvalidTockenException("Whatever")
}
And actually in that scenario, using "!" makes quite a bit of sense for me.
Finally, if no one can really explain WHY the rule is there, maybe it is time to remove it, the only good reason I could find for it would be to provide consistency regarding the code style.
Okay, I answer my own question.
As other say, maybe this is written for the readability.
In The Art of Readable Code (p. 72) says:
Prefer dealing with the positive case first instead of the negative-e.g., if(debug) instead of if(!debug)
I found below post as well:
Readable Code - Remove Checking null
bool func(String name)
{
if ( (name != null) && (name.equals("true") ) {
//...
} else {
//...
}
}
bool func(String name)
{
if ( "true".equals(name) ) {
//...
} else {
//...
}
}
Ofcourse you can use the negation operator ! whenever you like.
However, if you have a situation where you have to write some actions in both if and else block then the following is more readable :
if(status){
//do something
}
else{
//do something else
}
than
if(!status){
//do something
}
else{
//do something else
}
But if you have situation where you only need to perform certain actions based on just one condition, i.e. if you have only an if block & no else block, then it is reasonably fine to use ! in if
I haven't seen anyone else suggest this, which is probably because they hate it as much as I do, but I'm showing it for completeness.
// Using not operator (preferred)
if (! someTest) { ... }
// Using compact not operator (kind of hides it)
if (!someTest) { ... }
// Comparing to false (ok, explicitly states what you want)
if (someTest == false) { ... }
// Comparing to true (a bit obscure)
if (someTest != true) { ... }
They all do the same, but please keep using !, just make sure you add a space after it, so it's easier to see.

How can I check if anything is in an ArrayList

So I want to check if anything exists in an array list in an if statement. So instead of
MyArrayList.Contains("blah");
I want something that checks if ANYTHING is in it.
EDIT!:
Found it out!
Thanks for all the help!
Answer:
MyArrayList.isEmpty(){
}
THANKS!
List.isEmpty() is what you are looking for. Refer the docs.
You should to check if your ArrayList is not empty.
You can get achieve this like this code snippet:
if (!arrayList.isEmpty()) {
//your code here...
}
Just check the size of the list. If it's not zero, then there is something in it...
if(arrayList.size() != 0) {
//there is something in the list
}
List.isEmpty():
if (! list.isEmpty()) {
// do something
}
List.size():
if (list.size() > 0) {
// do something
}
Also, keep in mind that most of the time it's preferable to program against the interface (in this case, List) rather than the concrete class (e.g., ArrayList, LinkedList)
You could do it simply like this using .isEmpty():
if(!myArrayList.isEmpty()){
//if your array list has something in it
}
and,
if(myArrayList.isEmpty()){
//if your array list has nothing in it
}

Resetting and Stopping a function

I have a function that orders a movement to a sprite, how am I gonna reset it? whereas it will do the same order all over again, and after another condition is triggered it will stop
inside my function there is a boolean and a float.
MyFunction(float a, float b, boolean trig){
if(true){
//your condition/order
}
else
{
//do nothing
}
}
here is my code for resetting:
MyFunction(false or true).reset();
here is my code for stopping:
MyFunction(false);
is there something wrong in my code? or are there any better style of doing this, thanks in advance :)
you wrote if(true), instead if(trig)

Comparing strings in a Textbox

I would like to compare the string in a textbox if it contains "per/kg" and use that to disable a button. I have tried several methods but it did not work please kindly help out.
if (productDescTextBox.getText().equals("per/kg"))
{
buttonDot.setEnabled(true);
}
else buttonDot.setEnabled(false);
and this
if ("per/kg".equals(productDescTextBox.getText().toString()))
{
buttonDot.setEnabled(true);
}
else buttonDot.setEnabled(false);
use String.contains(CharSequence)
if (productDescTextBox.getText().contains("per/kg"))
Use string.contains.
if (productDescTextBox.getText().contains("per/kg")){
//Whatever you want
}
Note the method is case sensitive

Categories