Why is Eclipse stepping into when I hit step over? - java

I am using debug mode in Eclipse Oxygen to, as you might guess, debug my code.
I am writing a backtracking algorithm (a recursive function - it calls itself).
In the Backtrack function there is a for loop, and at the end of the for loop, if certain conditions are met, this code is run: Backtrack(csp, index + 1, CopyCSP(currentSolution));.
I'm debugging my code, and I want to go to the next iteration of the for loop, so when I get to this line, I hit "step over." But it steps into, and walks me through the next Backtrack function.
I know for a fact that it is actually the next function, because as you can see, the index variable goes up by one, which happened.
Why is this happening? How can I avoid this and actually step over? If step over doesn't do what I want here, what should I use?
Here's my code for the full function:
private void Backtrack(CSP csp, int index, CSP currentSolution) {
//BREAKPOINT IS HERE
if(index == csp.numVars) {
currentSolution.PrintSolution();
csp.PrintSolution(currentSolution);
solved = true;
return;
}
for(int test = 0; test < csp.MaxDomainSize(); test++) {
if(solved) {
return;
}
if(test < currentSolution.vars[index].domain.size) {
currentSolution.vars[index].value = currentSolution.vars[index].domain.get(test);
}
else {
continue;
}
boolean satisfied = true;
for(int i = 0; i < csp.constraints.size; i++) {
if(!csp.constraints.get(i).Satisfied(currentSolution.vars, index)) {
satisfied = false;
}
}
if(satisfied) {
System.out.println("Variable " + index + " satisfied by " + currentSolution.vars[index].value + ".");
Backtrack(csp, index + 1, CopyCSP(currentSolution));
}
}
}
I've put a comment where the breakpoint is.

I was having a similar problem and found similar solution, posting here for reference.
Problem:
Debug session 'Stepped Into' each line for every 'Step Over (F6)' operation
Solution:
'Run->Remove All Breakpoints'
'Run->Restart'
Result:
'Step Over (F6)' steps over active debug line now as expected.
Sharing for clarity and simplicity if encountered, hopefully this helps.

Following the train of thought that it's the breakpoint...
A breakpoint stops the flow of control in most situations.
Eclipse has options to disable individual breakpoints and disable all breakpoints. Your situation might be right for conditional breakpoints:
Right click on the breakpoint and select breakpoint properties. This will bring up a dialog with "hit count" and "conditional" which works most of the time and is confusing when it doesn't.
If you check "conditional", this will enable a text box where you can write a condition to use the variables to make a true statement. So you could enter "index==1000" and then it would stop when you are 1000 calls deep.
I have not used "hit count" myself.

Related

If statement seems to run some code but not all in java

I'm making a 2d platformer in libgdx with box2d. The below is the update method for one of my enemies. It's an extension of the 'Enemy' class, which is what 'super.update' refers to. I want the enemy to run when the player is behind it or far away and to stop and shoot when the player is close to it and in front of it.
I try to achieve this by setting the speed (velocity.x) initially [depending on the enemy's direction], then setting whether or not it's shooting afterwards.
The problem I have at the moment is that the enemy doesn't run when the player is behind it. As you can see, I printed out a lot of strings to console to see when the velocity.x gets changed back to 0. According to the console, it happens in the last if/else pair of statements which are supposed to check how far away the player is and which direction the enemy is running. However, the console strings within those statements, the ones that say 'Shoot Left' or 'Shoot Right', don't get printed out. Despite this, the line that changes velocity.x must get run because the it's value changes according to the string output in the next line. The if statements at the top which check direction must get run as well because the console outputs within those statements get printed, and the output that says the velocity says the correct velocity (either 2 or -2).
What is going on? It seems like the IDE is running only one of the lines in the if statement. That's impossible so what am I missing here?
Thanks for any help.
public void update (float dt, Player player){
super.update(dt, player);
if (b2body.isActive()){
System.out.println(b2body.getPosition().x - player.b2body.getPosition().x);
System.out.println("After Enemy code: " + velocity.x);
if (getRunningRight()) {
System.out.println("Right");
velocity.x = 2;
}
else if (!getRunningRight()) {
System.out.println("Left");
velocity.x = -2;
}
System.out.println("After checking direction: " + velocity.x);
if ((b2body.getPosition().x - player.b2body.getPosition().x <= 2 &&
b2body.getPosition().x - player.b2body.getPosition().x >= 0) && !getRunningRight()){
velocity.x = 0;
System.out.println("Shoot left");
}
else if ((b2body.getPosition().x - player.b2body.getPosition().x >= -2 &&
b2body.getPosition().x - player.b2body.getPosition().x < 0) && getRunningRight()){
System.out.println("Shoot right");
velocity.x = 0;
}
System.out.println("After shooting: " + velocity.x);
}
}
In your first set of ifs, you evaluate getRunningRight() after you have already determined that it will be false (by the initial if failing), so there is no need to evaluate it again.
If you think you are doing the same thing in the second block, you are not; the expression in the inner if is not the opposite of the first one. That is, (A && B) && C is not the opposite of (!A && !B) && !C. Thus, it is possible for both expressions to be false.

Strange behavior iterating over lists with debugger

In the following code:
for (Iterator<MyClass> iterOuter = mList1.iterator(); iterOuter.hasNext();) {
mClass1 = iterOuter.next();
for (Iterator<MyClass> iterInner = mList2.iterator(); iterInner.hasNext();) {
mClass2 = iterInner.next();
if (...) {
continue;
}
else if (...) {
//code 1
}
else if (...) {
continue;
}
else if (...) {
//code 2
}
}
}
EDIT: code 1 & 2 are math calculation, in any moment ain't add or remove to the iterators.
When I inspect it with debugger I notice then after the last inner iteration, when I expect the debugger jump to the inner for line check the condition and then realize it's end of loop and jump back to outer for loop.
Instead after the inner for check (when the condition is not met and need to jump back to outer) the debugger jumps to the line with //code 2 , NOT executing the code there and then jumps back to outer for.
What's happening here? Is this some issue with the debugger or it's something with java, the if else if structure with the continue etc?

Nothing is being output to the console! (Java, Eclipse Mars)

So basically I am experimenting with writing a path finding program that find a path from some point in a 10*10 grid to another, that is fine.
I have a class Path that is an ArrayList of GridSquares (which are just glorified co-ordinates).
I have written a small method within the Path class to display the path, and this is where the problem, which is so minor but so very infuriating, arises.
When I try to run the code, and call displayPath, nothing is output to the console and the program terminates with no errors.
Here is the code for displayPath:
public void displayPath(){
System.out.println("This is displayPrint"); //This line is included to make sure the program calls the method correctly.
for(int i=1; i==10; i++){
for(int j=1; j==10; j++){
if(this.includesSquare(i, j)){
System.out.print("[x]");
} else {
System.out.print("[ ]");
}
}
System.out.print("\n");
}
}
I included the first line to ensure that the console/System.out.print() was working correctly and this gets displayed every time the method is called.
Here is the code for includesSquare:
public boolean includesSquare(int x, int y){
for(GridSquare square : this.path){
if(square.getX()==x && square.getY()==y){
return true;
}
}
return false;
}
I have uninstalled and re-installed Eclipse, copied the java files into a new project ect and nothing seems to make any difference. I know the console is working fine as it displays the first line of displayPath correctly.
Any help is greatly appreciated!
for(int i=1; i==10; i++) and for(int j=1; j==10; j++) will not work.
The middle condition (i==10) is supposed to say when the loop is supposed to be executed. As it is, you are saying you only want the loop to execute when i is equal to 10. Since i is initially equal to 1, it will skip right over the loop.
What you will likely want is
for(int i=1; i<10; i++)
This way, when i is equal to 1, it satisfies the condition that it is less than 10, so the loop will get executed and i will increment. This will keep happening until i is equal to 10, at which point the condition i<10 fails, so the loop will exit.
In less words, you want your condition to say "loop while this is true" as opposed to "loop until this is true".
for(int i=1; i==10; i++){ is where your problem lies.
The syntax for the for loop is as follows:
for(<initial condition>; <checking condition>; <incrementing>)
So what you have is
Staring from i = 1, increment by 1 while i == 10. Well since i starts at 1, you've already failed at the first step!
Turn your for loop into while loop to understand this better:
int i = 1;
while(i == 10) {
doSomething();
i++;
}
So of course that won't work.

Java program won't finish method - exits after a for loop with no errors instead

edit: As I was writing this post, I made my code simpler (lost arrays entirely) and got it working. Yet I am still not sure why this specific code won't work, so I'll keep the question.
Hello.
I am writing a small puzzle game in Java (using Eclipse 4.4.2) and stumbled upon a problem inside one of my methods. Basically - it won't complete the method, it just exits the method after the for loop is done without any warnings or errors (I'm not catching exceptions either). I hope I missed something simple..
Details:
I have a method to set the colors of an object and up to 5 other objects that are linked to it through lines. I set up the color of the main object, then find the linked objects through for-loops and in the end change their colors as well. (Double checked the code for Lines, there are simple return methods and nestA and nestB as data - no problem there). lines is an array with a length of 50, nests as its members.
Here's the code:
public void highlightNests(Nest nest) {
//setting the color of the main object (a nest).
Mappers.setColor(nest, nestHighlight);
//resetting the array. Temp solution, had a return method earlier,
//this is part of the debugging.
connectedNests = null;
connectedNests = new Nest[5];
int i = 0;
Gdx.app.log("highlightNests()", "starting the loop");
for (int j=0; j<lines.length; j++) {
if (lines[j].getNestA() == nest) {
connectedNests[i] = lines[j].getNestB();
i++;
}
if (lines[j].getNestB() == nest) {
connectedNests[i] = lines[j].getNestA();
i++;
}
}
//This is where the program exits the method. The following
//lines are not run.
Gdx.app.log("highlightNests()", "entering loop");
for (int l=0; i<connectedNests.length; l++) {
Mappers.setColor(connectedNests[l], nestHighlight);
Gdx.app.log("highlightNests", "set color");
}
}
Deleting the middle section makes the end part run, so there are no errors in the last part.
Your second loop is completely wrong, you declare the counter as l and increment another counter i, you should use l<connectedNests.length change it like this:
for (int l=0; l<connectedNests.length; l++) {
Mappers.setColor(connectedNests[l], nestHighlight);
Gdx.app.log("highlightNests", "set color");
}
And the program won't finish method and exits before the loop because, it doesn't even enter the loop as it's incorrect.
You have to use l instead of i in condition like,
for (int l=0; l<connectedNests.length; l++)...
//---------^ l not i

Java help regarding looping (do loops)

I'm trying to make a very basic game where you guess a number between 1-1000 using a do loop. Everything works, except when I finally make the correct guess, I am still prompted to make another guess, and when I enter the same correct guess again, the program terminates like it's suppose to.
Why do I have to make that extra guess to finally get my program to work? Am I looping around an extra time? Also, if I make a correct guess (the compiler will say I am correct then still prompt me), then a wrong guess (the compiler will tell me I'm wrong), then the correct guess again, the program will only terminate after I make the correct guess a second time.
The second do loop at the bottom is what I put in my main method. Everything above is in a method I wrote called play.
public static boolean play()
{
boolean c;
int n = 0;
do {
String input = JOptionPane.showInputDialog("Enter a number between 1-1000");
n = Integer.parseInt(input);
if (n == guess)
{
System.out.println("Correct");
c = true;
}
else if (n < guess)
{
System.out.println("Not Right");
c = false;
}
else
{
System.out.println("Not Right");
c = false;
}
guess++;
} while (c == false);
return c;
}
In main method:
do {
game1.play();
} while (game1.play() != true);
This loop runs the play method twice in each iteration of the loop :
do {
game1.play(); // first call
} while (game1.play()!=true); // second call
You are not testing the value returned by the first call, so even if it returns true, you would still call game1.play() again, which will display "Enter a number between 1-1000" again.
Replace it with:
boolean done = false;
do {
done = game1.play();
} while (!done);
This would only call play() one time in each iteration of the loop.
That said, I'm not sure why you need the outer loop.
You can just replace in with one call to game1.play(), since game1.play() will loop until the correct number is entered.

Categories