Unknown while loop usage - java

This page a user must choose between one of 2 checkboxes 5 times. So I wrote this:
if (box1a.isSelected() == true || box1b.isSelected() == true) {
if (box2a.isSelected() == true || box2b.isSelected() == true) {
if (box3a.isSelected() == true || box3b.isSelected() == true) {
if (box4a.isSelected() == true || box4b.isSelected() == true) {
if (box5a.isSelected() == true || box5b.isSelected() == true) {
with some other things he does when it is true.
} else {
new Error("You must select an answer at all the questions");
}
Then he only returns a error if you don't check one of the top checkboxes. Then cleary I need a while loop in there but i don't know how to uhm do it. I know how a while loop works but don't know how It would look in this situation. Please help
Also now I have to do the same with text fields and using th same methode that I got answered by you guys doesn't work. any advise?

if ((box1a.isSelected() || box1b.isSelected()) &&
(box2a.isSelected() || box2b.isSelected()) &&
(box3a.isSelected() || box3b.isSelected()) &&
(box4a.isSelected() || box4b.isSelected()) &&
(box5a.isSelected() || box5b.isSelected()))
{
//true stuff
}
else
{
new Error("You must select an answer at all the questions");
}
You should never shouldn't test for true with ==. It is poor style, better to just use the return value from isSelected()

if ((box1a.isSelected() == true || box1b.isSelected() == true) &&
(box2a.isSelected() == true || box2b.isSelected() == true) &&
(box3a.isSelected() == true || box3b.isSelected() == true) &&
(box4a.isSelected() == true || box4b.isSelected() == true) &&
(box5a.isSelected() == true || box5b.isSelected() == true)) {
//DO SOMETHING IF TRUE
}
else {
new Error("You must select an answer at all the questions");
}
No looping needed ^_^

why don't you use radio button (with a default radio button checked) in this case ?

A general strategy would be something like this:
bool flag = true;
do{
//search for input
if (/*the input is valid*/)
flag = false;
}while (flag);
But if you hard code so many options, you might have the wrong design. Try something like a radio button like Jerome C. suggested.

if(!box1a.isSelected() && !box1b.isSelected()) {
// You must select an answer at all the questions
}
else if (box1a.isSelected() && box1b.isSelected() && box2a.isSelected() && box2b.isSelected() && box3a.isSelected() && box3b.isSelected() && box4a.isSelected() && box4b.isSelected() && box5a.isSelected() && box5b.isSelected()) {
// with some other things he does when it is true.
}
A few points to note here.
Avoid using class names like Error as they're normally used for genuine java.lang.Error logic.
If you have a boolean, you don't need to use a == operator.

Not sure why you want a while-loop. If you are thinking that the user must "stay in the loop" while the your condition (all 5 questions answered) is not met, then it is unnecessary. The Event Dispatch Thread (EDT) will continue running the "loop" for you.
On the other hand, if you are looking for a compact way to verify all of your checkboxes, you can change how they are declared from (assuming) javax.swing.JCheckbox box1a; etc. to either a fixed array or an ArrayList which you can then iterate over with a for-loop.

Related

If else condition combine && and || in one row statement

Thank you for your time, i create some if else statement in checkbox to display result, can i combine && and || condition in one statement? for example
if (radioMale && chestPain && (leftArm || bothArm || jaw || throat)) {
highPossibilityOfHeartDisease = true;
}
User have to tick radioMale && chest pain && can tick either leftArm, bothArm, jaw or throat (one or more) to return true for highPossibilityOfHeartDisease. Is the code above valid? need some help here.
Yes you can combine && and || in a if...else statement. See it logically before considering programmatic side.
true AND true AND true AND (true OR false OR false) the condition inside brackets will be verified and set as one resulted Boolean that may be true or false according to the condition.
Then the resulting booleans will be verified linearly as normal.
You can read some articles explaining maths of boolean expressions, for example:
The Mathematics of Boolean Algebra: From StanFord University
Boolean Expressions
Boolean algebra

Java and and or operator not working

I am using the following statement to break out of the loop when two conditions met:
while (true)
{
if (uAnswer1.equals(answerB1) || uAnswer1.equals(answerB2)
|| uAnswer1.equals(answerB3)|| uAnswer1.equals(answerB4)
&&
uAnswer2.equals(answerS1)|| uAnswer2.equals(answerS2)){
break;
}
The loops breaks when one or both && conditions are met. However, I wrote the code to break the loop ONLY when both conditions are true.
Is there something missing from above statement?
Regards,
Shei7141.
wrap them in parentheses
if ( (uAnswer1.equals(answerB1) || uAnswer1.equals(answerB2)
|| uAnswer1.equals(answerB3)|| uAnswer1.equals(answerB4))
&&
(uAnswer2.equals(answerS1)|| uAnswer2.equals(answerS2)) )
or
even make a HashSet of correct answers and do this will be clean and will be efficient too
answers1Set.contains(uAnswer1) && answers2Set.contains(uAnswer2)
above code shows that uAnswer1.equals(answerB4) && uAnswer2.equals(answerS1) are in AND condition
while (true)
{
if ((uAnswer1.equals(answerB1) || uAnswer1.equals(answerB2)
|| uAnswer1.equals(answerB3)|| uAnswer1.equals(answerB4))
&&
(uAnswer2.equals(answerS1)|| uAnswer2.equals(answerS2))){
break;
}
while (true)
{
if ((uAnswer1.equals(answerB1) || uAnswer1.equals(answerB2)
|| uAnswer1.equals(answerB3)|| uAnswer1.equals(answerB4))
&&
(uAnswer2.equals(answerS1)|| uAnswer2.equals(answerS2)))
break;
}

Multiple operators AND OR - Java

My code don't work. I expected to get the alert "You have xyz ....". Nothing happen. Its something wrong in the code?
if ((event.values[0] == 1 || event.values[0] == 2 || event.values[0] == 3)&&
(event.values[1] == 3 || event.values[1] == 4 || event.values[1] == 6)&&
(event.values[2] == 7 || event.values[2] == 8 || event.values[2] == 9)) {
values.setText("You have xyz ....");
}
Syntactic I don't see any obvious errors, however why you don't see your alert must be a result of incorrect values in your event[] variable. Have you tried debug your code?? To be sure what the values are?
Most probably the variables are wrong. I don't see a mistake in your code.

Operator to choose upon the condition

I have a table which contain two columns in database status and diff_id status column may contain these values
DFG_SGG_RGRG
NULL
EF_SFEG_FTT
IFEF_RGG
abc_id may contain these values
null
43546
45346
45746
53465
Now I am getting these values in an object t ,so I have to make an condition where status column is null and abc_id should have value 435465666L so I have prefer to write an if like shown below please advise is it correct as I am confused between && operator || operator
if ( if f.getStatus()== null || abc_id() .longValue()==435465666L )
{
//perform the logic
}
Please advise - is it correct approach?
& = "and"
&& = "and, but only if preceding condition was true"
| = "or"
|| = "or, but only if preceding condition was false"
If you're saying:
AND - use &&
OR - use ||
Remebmer also that && takes precedence over ||.
If both conditions must be true you need to use the && operator.
if (f.getStatus()== null && abc_id().longValue()==435465666L )
{
//perform the logic
}
Using the && operator causes a condition to be true only if both conditions are true. For example:
System.out.println(true && false); //prints false
System.out.println(false && false); //prints false
System.out.println(true && true); //prints true
System.out.println(false && true); //prints false
The || operator causes a condition to be true if one of the conditions is true.
System.out.println(true || false); //prints true
System.out.println(false || false); //prints false
System.out.println(true || true); //prints true
System.out.println(false || true); //prints true
use
if (f.getStatus()== null && abc_id().longValue() == 435465666L )
{
//perform the logic
}

How to call a Java function in the 'when' part of the drools rule?

I have been searching for a while now, but I can't find the exact answer to my question anywhere, or if I find something similar, it doesn't works.
I want to call a simple java method in the when part of the rule.
My code looks like this:
rule "Ret Rule"
when
Map(this["LOYAL"] == "true")
Map(this["LOYALTYPROMORETENTION"] == "true")
PromotionValidityPeriod(promotionName == "VIVACLUB Loyalty Promo 2013 25 percent")
$customer: Customer( segment == "Residential" , $assets : assets )
$o: Order( ( (DPOrderType == 17 && retentionReason == "RET") || (DPOrderType == 2 && reason == "557") ) , $ct: contractTerms == 24, $olis: orderLineItems )
$tariff: OrderLI( part in ("DT2319", "DT2320"), actionCode not in ("Delete", "INVALID"), $parentId : parentId) from $olis
OrderLI( part == "DT2316", nodeId == $parentId, actionCode not in ("Delete", "INVALID"), $assetId : assetId ) from $olis
/*Asset( assetId == $assetId,
( (contractTerms != null && contractEndDate != null && eval(CalculationsHelper.getFullMonthDifference(new Date(), contractEndDate) < 3 ))
|| (contractTerms == null) ) ) from $assets*/
$li : OrderLI( $newTariff : part in ("DT2319", "DT2320"), parentId == $parentnodeid, actionCode == "Add") from $olis
$del : OrderLI( $oldTariff : part, parentId == $parentnodeid, actionCode == "Delete", productType == "Calling Plan") from $olis
eval(OrderDwrController.setTransitionCondition(fromTariff == $oldTariff, toTariff == $newTariff) == true
then
Offer of = new Offer("DT2331", $parentId, 7);
System.out.println($tariffOld);
of.getOrderLineItemAttributes().add(new OrderLIAttribute("DURATION", "" + $ct));
of.getOrderLineItemAttributes().add(new OrderLIAttribute("Discount of MRC", "25%"));
of.getOrderLineItemAttributes().add(new OrderLIAttribute("VIVACOM TV Package", $tariff.getProductNameENU()));
of.setProductNameENU("VIVACLUB Loyalty Promo 2013 25 percent");
$o.addOffer(of);
of.setLoyaltyPromo(true);
$o.addTextForOffer(of, new Integer[]{173});
end
The particular line where I have a problem is the very last one in the when part:
eval(OrderDwrController.setTransitionCondition(
fromTariff == $oldTariff, toTariff == $newTariff) == true
I just want to call a simple function
(OrderDwrController.setTransitionCondition(
fromTariff == $oldTariff, toTariff == $newTariff))
like the one above mine
(eval(CalculationsHelper.getFullMonthDifference(
new Date(), contractEndDate) < 3 ))
The function is static, returns a boolean value. I have imported the class in the beginning of the file.
What am I doing wrong?
1st of all you didnt close the eval().
2nd if you upgrade your drools you could just write java expressions in the then section and it'll be faster than eval()
Sorry for posting an answer instead of just replying to you (Don't have enough reputation yet :))
Whatever you put inside an eval must evaluate to a boolean. I am not sure yours is. That could be the problem.
Hope that helps
Cheers,
Avinash

Categories

Resources