my code is
int count = cur.getCount();
if (count > 0) // condition is true
return true;
else
return false; // excute this statement
here count value is 1. and in if condition "count > 0" returns true. but also it jumb to else statement. if statement not excute. it returns false. can any one give a solution for this?
Change it to
if (count > 0) {
return true;
} else {
return false;
}
or just put:
return count > 0;
I've had this issue when special characters got in.
Delete everything and rewrite it (don't copy paste) and see if it still happens.
Also, try placing a watch on Count and see what the value is to identify exactly why the branch happens...
Related
I have four methods that check whether or not a given grid location is next to an occupied location (value of 1). The grid is assumed to wrap around, ie, if in a 50x50 grid[0][1] is the given location and grid[49][1] is occupied, the method should return true/ My checkNorth and checkEast method are working fine, but I get an ArrayIndexOutofBoundsException: -1 error for either the south or west methods every time I run the program. I checked my math and I think it should work - am I using the modulo incorrectly, or am I missing something else?
EDIT: Clarified the wrapping criterion, word use correction.
boolean checkWest(int indexA, int indexB)
{
if (indexA-1 > 0)
{
if (grid[indexA-1][indexB] == 1)
{
return true;
}
}
if (indexA-1 < 0)
{
if (grid[(indexA-1)%width][indexB] == 1)
{return true;}
else return false;
}
return false;
}
I see a couple problems. First, Java arrays are zero-indexed, which means that the first element is at index 0. So it's okay to check grid[indexA-1][indexB] when indexA-1 is equal to 0. Second, you're not properly handling when indexA equals 0. Here is my implementation. I also simplified the logic a bit.
boolean checkWest(int indexA, int indexB)
{
if (indexA > 0)
return grid[indexA - 1][indexB] == 1;
else
return grid[width + indexA - 2][indexB] == 1;
}
EDIT: I'm pretty sure I butchered the math with the second return statement. It should be right now...
I got a java.lang.ArrayIndexOutOfBoundsException and I think it is because when nums.length gives me a zero (because there is nothing inserted in the parameter) the attribute I gave to the variable (-----.length -1------) will make it -1 when it gives me zero.
How should I make it not do this knowing that if I leave it .length without a -1 there will be an out of bounds exception on the other side of the array?
public boolean sameFirstLast(int[] nums) {
int lengthOfArray = nums.length - 1;
int thisIsHowLong = nums.length;
if (nums[0] == nums[lengthOfArray ] && thisIsHowLong > 0){
return true;
}
return false;
}
//always remember to return false in the end becuse if the top dosnt run then
//there will be no boolean returned and the method is not void so it has to return a //boolean
//ALWAYS REMEMBER TO SET nums.length - 1
//the .length method always starts from 1 and threfore will give you an extra space //that it will check for but we wont have anything in that space and then the compiler //will go to that space and will fond nothing to test with and gove an ERROR out of //bounds exeption because it is looking out of the array.
Your condition should look like
if(thisIsHowLong > 0 && nums[0] == nums[lengthOfArray ] )
Now if your first condition is false, second will never be checked.
And you want same.
You need to interchange the conditions.
if (thisIsHowLong > 0 && nums[0] == nums[lengthOfArray ]){ // This will prevent the exception.
I have a boolean method in my Java program and I'm wondering why NetBeans is recommending making this change to my code:
return isPrime != 0;
What I wrote was:
if (isPrime == 0) {
return false;
}
else{
return true;
}
Both work correctly but I cannot understand the logic behind the change NetBeans is suggesting. Neither true or false is being returned. Could someone explain to me the logic behind this? Thanks
NetBeans is exactly correct. The expression is a boolean. You can easily prove it by making the change and trying it.
What value does 0 != 0 evaluate to? (Hint: it's false). What about 1 != 0? (Hint: it's true).
Is that not what your more verbose code says?
Instead of
if (isPrime == 0) {
return false;
} else {
return true;
}
try
return (isPrime != 0);
It is the same thing, I'll show you the refactoring path. Starting with
if (isPrime == 0) {
return false;
} else {
return true;
}
is the same as
if (isPrime != 0) {
return true;
} else {
return false;
}
which can be reduced by substitution, substituting 'isPrime != 0' with the function 'doIsPrime()'
private boolean doIsPrime() {
return isPrime != 0;
}
substituting in
if (doIsPrime()) {
// guaranteed to be the same as above!
return doIsPrime();
} else {
return doIsPrime();
}
which can have both blocks reduced (as duplicated code)
if (doIsPrime()) {
}
return doIsPrime();
And reduced further by removing the if statement around the empty block
return doIsPrime();
Now undo the substitution 'doIsPrime()' back to 'isPrime != 0'
return isPrime != 0;
There was no need to really do the substitution; but, I find it better shows off the reasoning the if statement is redundant.
Neither true or false is being returned.
False. The result of the comparison isPrime != 0, a boolean is being returned, either true or false.
Could someone explain to me the logic behind this?
The first code is equivalent to the second code. If isPrime is 0, then return false, else return true. The != operator will yield false if isPrime is 0, and true if it isn't 0.
It means its returning the result of the predicat isPrime != 0
If isPrime = 0, then the predicat is false, therefore it return false
If isPrime != 0, then the predicat is true, therefore returning true
It's just to reduce code.
The expression isPrime != 0 returns a boolean. It is false if isPrime == 0 and true if isPrime != 0. Thus you can save the if statement and some lines of code.
Just to add to what's already said:
Though your solution and NetBeans recommended approach are exactly correct, one other approach would also be:
if (isPrime == 0) {
return false;
}
return true;
I don't know what NetBeans would suggest with this approach but I guess that it would propose the same recommendation as it did above.
If you have two if statements for:
int n = -1;
if (n < 0)
return null;
if (n <= 1)
return "yay";
Will it return null or yay? Will it run through the code top to bottom and stop at the first if statement, or will the last one be used?
The first if statement will be evaluated first, of course. n which is -1 is in fact < 0, so the body of code associated with that if statement will be executed. null will be returned.
What might be confusing you is that, though the second if statement would evaluate true, it will never be evaluated. This is because a return statement, when executed, leaves the function/method it is inside of.
If you wrote:
int x = 0;
return x;
x = 5;
x would be set to 0. x would be returned. Any lines of code after the return would never execute.
Here's another example, to clarify:
int x = 10;
if(x < 0)
return;
x = 0;
x would be set to 10. x which is 10 is in fact not < 0, so the if statement's body will be skipped. x would be set to 0.
it will return null, only because returning (in the first test) means the 2nd test will not be executed.
it will return null, as it's the first condition that matches. After the return the rest of the function isn't executed, and a function is executed sequentially, so they will never conflict.
Another sample of 'conflicting if statements' is when you create code that cannot be reached, for example:
if (n < 0)
return "a";
if (n == -1)
return "b";
This code will never return "b", and the compiler will probably error or warn about it. The reason is that when n = -1, the first statement is always hit, so the second statement is never reached.
It returns null, because it is the first return it encounters. It doesn't even check the second exception.
When the code is run, JVM checks condition in the first if and, when condition is met, it executes what's inside this if. If there is return null then it returns null. After return no code is executed in this method. The only exception is when you have return in try block and finally block after this try.
This is just a small re-format of your code with some explicit comments. I find that the explicit braces help some people see what is going on. Just follow the program flow. If the function is returned from, then no subsequent statements in the function are ever executed. (This is a white lie, see 'finally' Exception handling, but barring that...) -- it's a very procedural (step-by-step) process.
int n = -1;
if (n < 0) {
return null; // no other statement in this function will be executed
// if this line is reached
}
if (n <= 1) {
return "yay"; // no other statement in this function will be executed
// if this line is reached
}
It'll return null.
btw in Java "If" is invalid, you have to use "if" and round brackets for ifs
Will it run through the code top to bottom and stop at the first IF statement
Yes, it will. If it does turn out that n < 0, then it will immediately return null. (I don't know why you tagged this question as Java though, the code you posted is not Java code.)
Will return null. Once you hit the return statement control passes back to the calling function setting the return value of the function to what you specify after the return keyword.
It will return null. The only value for the code you posted that would ever result in 'Yay' being returned would be 0.
I am debugging the following lines of code
if (var.getvar2() != var3) {
var4.add(var);
} else {
isNeeded= true;
if (incomingPublishedDate.compare(modifiedDate) < 0) {
importNeeded = true;
} else {
var4.add(var);
}
}
Here var.getvar2() and var3 are of type Long.
While debugging, when the condition goes like
10000 != 10000
the if should evaluate to false. But from the first if, the next Step Over goes to
var4.add(var);
and the next Step Over goes to var4.add(var);
Is this a Netbeans bug? Or is it with the Long comparision.
I am using Netbeans IDE 6.5
You cannot compare objects by value. That comparison would only be true if the two references compared refer to the same object. Instead use:
if (! var.getvar2().equals(var3)) {
...
}