i've a problem with a couple of if-else conditions, here's the code:
if((hour<=16 && min<30)||(hour>=21 && min>0))
{ //copy of if#3
Log.d("baja", "copy");
message="something2";
}
if ((day>=1 && month>=4) && (day<=30 && month<=9))
{ //if#1
if((hour<=16 && min<30)||(hour>=23 && min>0))
{ //if#2
message="something";
}
}//end of if#1
else
{ //else for if#1
Log.d("baja", "before if#3 ");
if((hour<=16 && min<30)||(hour>=21 && min>0)){ //if#3
Log.d("baja", "if#3");
message="something2";
}
}
the problem is that if the flow enters the else if#3 doesn't work, but the copy i've put outside if#1 works perfectly....what is the problem?
i can't post the exact log now, but i can see "copy" and "before if#3"
I think I know what you are trying to do (If I understand your question correctly). The reason for this is that the else block is attached to the high level if block, rather than it's internal logic. If the if#1 returns false, nothing will happen, if it is true it will test if#2 -> if this is false, it will execute the else statement. If this is not your question, please supply the stack trace as rattmuff requested
if ((day>=1 && month>=4) && (day<=30 && month<=9))
{
if((hour<=16 && min<30)||(hour>=23 && min>0))
{
message="something";
}//if#2
else
{
Log.d("baja", "before if#3 ");
}//else
}//if#1
else if((hour<=16 && min<30)||(hour>=21 && min>0))
{
Log.d("baja", "if#3");
message="something2";
}//if#3
Related
I've run into a problem with my multi branch if...else statements. In the challenge question they ask us to print out a line if certain variables are true|false. My statements will only print my if statement, however they compile. I feel I'm either missing something in my if statement that allows it to continue if a statement is not true|false.
Here is what I have:
if ( isBalloon && isRed ) {
isBalloon=false;
isRed=false;
System.out.println("Not a balloon");
}
else if ( isBalloon && isRed ) {
isBalloon=true;
isRed=false;
System.out.println("Balloon");
}
Also, for clarity; when we do a multi branch statement (else if) requires variable declaration, where as (else) is just anything that makes our if statement false. Is this correct?
I think this is easiest explained by pointing out that 'else if' in java isn't a separate keyword. So it is equivalent to the following (which is harder to read but points out why your code is executed only once)
if ( isBalloon && isRed ) {
isBalloon=false;
isRed=false;
System.out.println("Not a balloon");
} else {
if ( isBalloon && isRed ) {
isBalloon=true;
isRed=false;
System.out.println("Balloon");
}
}
this is what else if really does. When your code runs it hits the first if condition and when it is correct it will run the associated block. it then doesn't process the else block. (that is the idea: only if or else runs, not both).
It looks like if / else if / else if / else has multiple blocks on the same level but secretly they are nested.
for instance
if (a) {
// case a
} else if (b) {
// case b
} else if (c) {
// case c
} else {
// otherwise:
}
is actually:
if (a) {
// case a
} else {
if (b) {
// case b
} else {
if (c) {
// case c
} else {
// otherwise
}
}
}
I was working on a game called the L game. In the function to check for a win, I had an if statement like this:
if (buttons[i][0].getText().equals(colour) || buttons[i][0].getText().equals("0") && buttons[i][1].getText().equals(colour) || buttons[i][1].getText().equals("0") && buttons[i][2].getText().equals(colour) || buttons[i][2].getText().equals("0") && buttons[i+1][2].getText().equals(colour) || buttons[i+1][2].getText().equals("0") && !(buttons[i][0].getText().equals(colour) && buttons[i][1].getText().equals(colour) && buttons[i][2].getText().equals(colour) && buttons[i+1][2].getText().equals(colour))) {
return false;
}
And this code didn't work. Not that I was getting an error, just it was not doing what it was supposed to do when a player won. However changed it to a few if statements in each other like this:
if (buttons[i][0].getText().equals(colour) || buttons[i][0].getText().equals("0")) {
if (buttons[i][1].getText().equals(colour) || buttons[i][1].getText().equals("0")) {
if (buttons[i][2].getText().equals(colour) || buttons[i][2].getText().equals("0")) {
if (buttons[i+1][2].getText().equals(colour) || buttons[i+1][2].getText().equals("0")) {
if (!(buttons[i][0].getText().equals(colour) && buttons[i][1].getText().equals(colour) && buttons[i][2].getText().equals(colour) && buttons[i+1][2].getText().equals(
return false;
}
}
}
}
}
And this does work.
Your two code snippets behave differently not because you have exceeded some "maximum characters in an if statement" limit, but because && has a higher precedence than ||.
When you say:
A || B && C || D
You meant
(A || B) && (C || D)
But without any parentheses, Java thought you meant:
A || (B && C) || D
This is because && has a higher precedence than ||. It's kind of like how you do multiplication first, than addition.
That aside, there is theoretically no limit on how long an if condition can be. It is not specified in the Java Language Specification. As long as you have enough RAM for the compiler, disk space to store the source file, and time for the compilation process, your code should compile eventually, if we assume the compiler implements the spec perfectly.
This doesn't mean that you should be writing super long if statements, though. Code is not only read by computers. Arguably, it is more often read by people than computers. So please keep that in mind when writing code.
A first step to refactoring your code would be to write a method like this:
private bool isButton0(int x, int y) {
return buttons[x][y].getText().equals("0");
}
so that you don't have to repeatedly say buttons[i][1].getText().equals("0").
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;
}
Obviously I'm missing something basic here. I have a function where there are return statements all over the function in various if statements. The benefit of that can obviously be debated but I'm running into a situation which I'm not familiar with.
I followed the execution of my program through break points and noticed it proceed as normal to a return statement. However, after reaching that statement it skips the remainder of the function and goes to the return statement at the bottom of the function and returns the value sitting down there. Here are the code snippets, it is hundreds of lines long so I don't want to post the whole thing, just the parts I traced it through:
public Location getBestLocation(Context c){ //the header
else if(gps_enabled && passive_enabled && !network_enabled){
if(hGPSLast != null && hPassive != null){
if(hGPSLast.getTime() > hPassive.getTime() && System.currentTimeMillis() - hGPSLast.getTime() < 300000){
return hGPSLast;
}else if(hPassive.getTime() > hGPSLast.getTime() && System.currentTimeMillis() - hPassive.getTime() < 300000){
return hPassive;
}else{
hGPSBest = getGPSloc(c);
if(hGPSBest.getTime() == hGPSLast.getTime()){
if(hPassive.getTime() > hGPSLast.getTime()){
return hPassive;
}else{
return hGPSLast;
}
}else{
return hGPSBest;
}
}
}else if(hGPSLast != null && hPassive == null){
if(System.currentTimeMillis() - hGPSLast.getTime() <300000){
return hGPSLast;
}else{
hGPSBest = getGPSloc(c);
return hGPSBest;
}
}else if(hPassive != null && hGPSLast == null){
if(System.currentTimeMillis() - hPassive.getTime() < 300000){
return hPassive;
}else{
hGPSBest = getGPSloc(c);
if(hGPSBest != null){
return hGPSBest;
}else{
return hPassive;
}
}
}
one of the returns in that part of the body is reached, however then the code skips to the very bottom where I have this:
Criteria criteria = new Criteria();
String best = lm.getBestProvider(criteria, true);
//since you are using true as the second parameter, you will only get the best of providers which are enabled.
def = lm.getLastKnownLocation(best);
return def;
The code is returning "def" which I defined as a blank holder Location object which I was hoping would be filled by the code just above it if for whatever reason none of the returns in the body were reached. What am I missing here?
A return command immediately ignores all remaining method code and exits the current method it is in. It will immediately return to the former function where the method itself was called.
For example:
if(hPassive.getTime() > hGPSLast.getTime()){
return hPassive;
}else{
return hGPSLast;
}
can be rewritten as
if(hPassive.getTime() > hGPSLast.getTime()){
return hPassive;
}
return hGPSLast;
The method will always return either of the two and skip the remaining code.
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.