Netbeans 6.5 debug issue - java

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)) {
...
}

Related

Find out value that returns true from if statement -algorithm

Another class is going to pass in random numbers into this method(x,y,z). I want to know the boolean that does returns true from my last if() statement, so I can do operations on it. I have explained my logic in the comments.
I am still really new to this, so my logic may be wrong.
public static String FindDate(int x, int y, int z) {
boolean istrue1 =(x >= 1 && x <= 31);
boolean istrue2 =(y >= 1 && y <= 31);
boolean istrue3 =(z >= 1 && z <= 31);
if(istrue1 ^ istrue2){
if(istrue1^istrue3){
if(istrue2^istrue3){//now knowing that no values are the same, i can find the true value.
if(istrue1||istrue2||istrue3){
// I want to store/use/print/know which bool(istrue) that evaluated to true, so I would know if it is
//x,y,z that went through the algorithm successfully.
}
} else{return "Ambiguous";}
}else{return "Ambiguous";}
}else{return "Ambiguous";}
return "true"; //I would actually end up returning the value that went through the algorithm
}
You can store boolean values just like any other type in Java. I'm not sure exactly what your last comment means by "the value that went through", but if you want to keep track of the result of a particular test, you don't necessarily need to write something like
if (a == b) {
return true;
} else {
return false;
}
In that case,
return a == b;
is equivalent. If the expression is more complicated parens are a good idea.
return ((a == b) || c);
And instead of returning, you could always store the result and do something with it later.
bool test = ((a == b) || c);
action(test);
You can your following: But your condition is incorrect 3 variable for two value (true, false) all can't be different. So your logic always return "Ambiguous"
if(istrue1 ^ istrue2){
if(istrue1^istrue3){
if(istrue2^istrue3){
//now knowing that no values are the same, i can find the true value.
if(istrue1||istrue2||istrue3){
// I want to store/use/print/know the bool that evaluated to true, so I would know if it is
//x,y,z that went through the algorithm successfully.
return "true";
}
}
return "Ambiguous";
Instead of:
if(istrue1||istrue2||istrue3)
Its is easiest to just break it down into 3 differnt if statement.
if(istrue1)
if(istrue2)
if(istrue3)
No easy trick that i was hoping for. sad day.
Also the statements i did with the xor(^) operators turns out to be bad logic.
it would be easiest to this:
if(a&&b || a&&c || b&&c)
That would return ambiguous if any combo are both true.
However thats not the original question, but I thought i might as well mention it.

Wrap Around Grid - errors on east/west only

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...

Java code not hitting IF or ELSE in Android Studio

I am running Android Studio and setting breakpoints, but on both of my IF...ELSE conditions, the code is not being executed. It seems to me that such a thing is impossible. Either the IF or the ELSE should be true... right?
The code is this:
if (lastReading.isItTimeYet(Calendar.getInstance()))
{
lastReadingReturn = lastReading.SensorReadingChanges(z_value, chkOrient, inclination, rotation);
if (lastReadingReturn.isEmpty())
{
String EMPTY = "TRUE";
// DO NOTHING
}
else
{
int stopHERE = 0;
}
}
lastReadingReturn is a string. It is getting a value from SensorReadingChanges just fine. I don't see any errors being thrown.
I put break points on both the String EMPTY = "TRUE"; line and the int stopHERE = 0; line, but neither is hit. I can stop on the line before the if. But when I try to step into or step over the next line, the debugger jumps to the first line of code that is OUT of the If clause. In other words, it just skips it.
I have run it with both conditions (i.e. the string being checked is empty and the string being checked has a value) but it doesn't matter. Neither is hit.
Here is a screenshot of my Android Studio running in debugger:
Both your if-block and your else-block contains code that is very likely to be removed by the compiler during optimization since they actually don't do anything.
Try replacing them with something that actually does something like logging a message or move the declaration of EMPTY and stopHERE outside of their respective blocks.
String EMPTY;
int stopHERE;
if (lastReading.isItTimeYet(Calendar.getInstance()))
{
lastReadingReturn = lastReading.SensorReadingChanges(z_value, chkOrient, inclination, rotation);
if (lastReadingReturn.isEmpty())
{
EMPTY = "TRUE";
// DO NOTHING
System.out.println("Doing nothing");
}
else
{
stopHERE = 0;
System.out.println("stopHERE set to zero");
}
}
Edit: Since I'm not being believed I simulated OPs issue in Android Studio:
final Random random = new Random();
if(random.nextInt(1) != 0) {
String EMPTY = "EMPTY";
} else {
int stopHERE = 0;
}
So, Android Studio is actually warning us that there is no executable code inside our else-block where the breakpoint is.
When run, this will only pause execution once, on row 18 (since random.nextInt(1) will always be 0).

IndexOutOf range exception thrown in TimeHandler

I'm making a card-like game. The method below handles the opponents' attacks. It is supposed to go through the opponents' cards (with a two second delay between them) and execute their attacks (dealDamage() returns the damage). First it should check if the current opponent is active (i.e. not killed) and if it has already attacked, if so, their attack is executed. After that, the method moved to the next active opponent. After the last one it should stop iterating and change to the next round. Although the in first round everything goes as planned, but then it often crashes during the second or last round (when only one opponent is active). There can be 5 opponents max. I am clearly missing some obvious thing, but I can't get this to work.
EDIT
The error is in while(gm.opponentList.get(i).is available...
it is for examle index out of range: index 4 size 4.
public void opponentAttack(){
this.registerUpdateHandler(new TimerHandler(2f, true, new ITimerCallback() {
int i = 0;
#Override
public void onTimePassed(TimerHandler pTimerHandler) {
if (i < gm.opponentList.size()) {
while (gm.opponentList.get(i).isAvailable == false
|| gm.opponentList.get(i).attacked == true) {
i++;
}
if (gm.opponentList.get(i).isAvailable == true
&& gm.opponentList.get(i).attacked == false) {
gm.negotiationStrength = gm.negotiationStrength - gm.opponentList.get(i).dealDamage();
i++;
}
if (gm.negotiationStrength < 1) {
gm.negotiationEnded = true;
gm.playersTurn = false;
}
i = 0;
}else{
pTimerHandler.setAutoReset(false);
}
}
}));
gm.nextRound();
}
while (gm.opponentList.get(i).isAvailable == false
|| gm.opponentList.get(i).attacked == true) {
i++;
}
if the exit condition is never met, you keep incrementing i, and when it exceed the size of gm.opponentList throws an IndexOutBoundExcetion. You probably want to check i against gm.opponentList.size(), in the while loop

If condition not working properly in android eclipse

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...

Categories