How to set breakpoints at specific spots in Intellij? - java

I have a program that modifies images. When I try to compare the results of my program to the expected values the result is pretty much unreadable.
This continues for very very long, so how can I make it so that I only see the difference between the expected and actual values, and where these differences are?
And when using the inbuild debugger, for example if I have a loop
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
do stuff
}
}
How can I set up a breakpoint so the debugger can tell me the values of things I need to know to find out what the error is:
When i = 99
Right before a discrepancy between the expected and actual value occurs
Right before the program exits to an error such as out of bounds.
Because currently, all I can do set a breakpoint on a specific line, but inside a loop I have to click step over repeatedly rather than skip to where I need in the loop.

TLDR; right click on a breakpoint and add code that returns a boolean
to make it a conditional breakpoint
I think this post useful for your question.
conditional breakpoint in IntelliJ

Related

Debugger doesn't show new variable in stack frame

Can someone tell me, why the debugger doesn't show variable j in stack frame(Using IntelliJ IDEA)?
for (int i = 0 ; i < 10; i++) {
int j = 100;
}
when i = 0, variable j should be created once, then, being removed from the stack, then i = 2, j should be loaded into the stack, but IntelliJ debug tool doesn't show the creation of j once, Where is the problem? Thank you
Screenshot:
There is a tool, please try it out https://cscircles.cemc.uwaterloo.ca/java_visualize/#
This variable is not used and javac compiler optimizes the bytecode by completely removing it. You can add some usage for the variable, like log it so System.out and then you should be able to see it in the debugger.

What is the output of this code using a stack?

We're learning stacks in Intermediate Programming and we're using Practice-IT! for coding examples. I'm stuck on this one question:
"Write a method splitStack that takes a stack of integers as a parameter and splits it into negatives and non-negatives. The numbers in the stack should be rearranged so that all the negatives appear on the bottom of the stack and all the non-negatives appear on the top. In other words, if after this method is called you were to pop numbers off the stack, you would first get all the nonnegative numbers and then get all the negative numbers. It does not matter what order the numbers appear in as long as all the negatives appear lower in the stack than all the non-negatives. You may use a single queue as auxiliary storage."I tried writing some code for it but it's saying it's wrong. I don't know where I went wrong since I'm learning this for the first time.
public Stack<Integer> splitStack(Stack<Integer> intSt)
{
Stack posSt = new Stack();
Stack negSt = new Stack();
for(int i = 0; i<intSt.size(); i++)
{
intSt.pop();
if (intSt.peek() < 0)
{
negSt.push(intSt);
}
else
{
posSt.push(intSt);
}
}
for(int i = 0; i<negSt.size(); i++)
{
negSt.pop();
intSt.push();
}
for(int i=0; i<posSt.size(); i++)
{
posSt.pop();
intSt.push();
}
return intSt;
}
There are multiple issues with the code so I'm gonna tackle them one by one but first, I'm gonna talk about the root of them: you need to develop your code on an IDE that supports intellisense and debugging, for java, the one I like the most is IntelliJ.
With a good IDE, you'd have seem that:
pop() has a return value, so when you pop, you should store the popped value like int poppedValue = intSt.pop();
push requires a value, so if you want to push the value you just popped from intSt you should go with negSt.push(poppedValue); (you push poppedValue, not the intSt)
after fixing all your pushes and pops, the program would still give wrong results because intSt.size() changes as you pop the stack, so your for would exit before the stack was empty and thus, would not split the whole stack, you could fix this by changing your fors into whiles as in while(intSt.size() > 0)
The last one would be the hardest one to catch, so I'm gonna suggest you some homework that will help you start finding all those issues by yourself:
Download an actual java IDE (IntelliJ, Eclipse or Netbeans).
Understand those tools intellisense (all of them bring up a context menu when you hit period '.' after a variable name, that menu contains a lot of information about methods and properties of the variable)
Learn to set up a breakpoint.
Learn step-over, step-into and step-out.
Learn how to use the "watch" window.
After that, will never want to work directly on a site anymore, you'll always develop on an IDE and then copy your work somewhere else after it's done, it really saves you a lot of time.

Setting new-line rules in Eclipse

I want to make the format of my code (Java) look like this:
for (i = 0; i < 3; i++)
{
System.out.println(i);
System.out.println(i+1);
}
above is for code that have brackets.
For the code that have only one line:
for (i = 0; i < 3; i++) System.out.println(i);
I can do the first one but moving the System.out.println(i); to the same line as 'for', I cannot find the place to set.
Thank you in advance.
[Addition] I mean how to setting in preference so that when I press [Ctrl + Shift + F] it will format as what I want. Sorry for my bad explanation.
There seems to be no option. Someone submitted it as request in 2005 but it was never implemented https://bugs.eclipse.org/bugs/show_bug.cgi?id=104910
So your only option seems to be to enable the //#formatter:on (respectively :off) and do them around your for statements and format them manually. But this seems like too much work so I would just let Eclipse format it like it already does.

Why is my for loop telling me Syntax error, insert "AssignmentOperator Expression"?

I have some code, like so:
int batchPosition = new Integer(batchBegin);
for (batchPosition;batchPosition<=batchEnd;batchPosition++)
But I get this error in eclipse:
Syntax error, insert "AssignmentOperator Expression" to complete ForInit.
I've looked at various posts on SO about this error, and googled it but I can't figure out why this isn't allowed.
batchPosition on it's own is not a valid initialisation statement - you can simply skip it:
int batchPosition = new Integer(batchBegin);
for (; batchPosition <= batchEnd; batchPosition++)
But if you don't need to access batchPosition after your loop, it is good practice to reduce variables scopes as much as possible:
for (int batchPosition = new Integer(batchBegin); batchPosition <= batchEnd; batchPosition++)
For some reason Java or Eclipse (or bother) doesn't like this part of the loop:
for (batchPostion....
It expects the variable being used to count position (batchPosition) in the loop to be initialised in the loop header (the for(first;only when;repeat) part.) I would guess this is because wants it to only be local to the loop.
To fix just move you assignment into the header, like so:
for (int batchPosition = new Integer (batchBegin);batchPosition<=batchEnd;batchPosition++)
Not as pretty, but it will work.
for loop contains 4 parts of execution:
initialization, Condition, execution-body, increment or decrement
int batchPosition = new Integer(batchBegin);
for (batchPostion;batchPosition<=batchEnd;batchPosition++)
You've missed the initialization part.
Either ignore it at all cause before for you've already initialized
for (;batchPosition<=batchEnd;batchPosition++)
OR
Move the line before for to inside for
for (int batchPosition = new Integer(batchBegin);batchPosition<=batchEnd;batchPosition++)
but, in latter case, you won't be able to use batchPosition outside for scope.

Value (not index) greater than 3 in array causes java.lang.ArrayIndexOutOfBoundsException

I'm a novice programmer. This may be a simple problem but I've never seen this before. First of all, let me clarify that I'm not even trying to manipulate the index. Here's the part of the code that is causing the exception:
int[] bumpercatcher = new int[4];
//time variable that helps control events
int time = 0;
public void setup()
{
bumpercatcher[0]=4;
bumpercatcher[1]=4;
bumpercatcher[2]=4;
bumpercatcher[3]=4;
As you can see I'm trying to set them all equal to 4 at the start of the program. This causes the arrayindexoutofbounds exception. If I set them all equal to 0~3 then there is no problem (until I set them to a value greater than 3 later in the program). I don't understand it.
-it doesn't matter if I set the array size to 10, I still get the same exception
-it doesn't matter if I set only one of the values (i.e. at index 1, which is definitely within bounds of the array). same exception
Is there something I'm doing wrong? Thanks.
well, here' the entire code if you want to take a look(not too long, 1 class, bad programming practies): http://dl.dropbox.com/u/33501308/Pong.java
Here's the html from which you can see the program from (not much to see. it just freezes instantly.): http://dl.dropbox.com/u/33501308/bin.zip
by the way I'm using eclipse.
I don't really know what SSCEE is. sorry
Your posted code file includes loops along the lines of
for(int j: bumpercatcher) {
if(bumpercatcher[j]>5)
...
}
This is an issue. This is a different kind of loop than a traditional for loop. It is an extended or enhanced for, also called a foreach. It reads "for each integer j in array bumpercatcher do x." You are taking your element j (a value) and using it as an index to the array. When your value exceeds the maximum index, you will get an exception.
Write your code with a proper for loop if you want to access by index, or try simply restructuring your logic like
for (int j : bumpercatcher) {
if (j > 5) // j is the value!
...
}
I'm not sure what the problem is, but a far more readable way of doing this would be with a for loop:
for(int i = 0; i < bumpercatcher.length; i++) {
bumpercatcher[i] = 4;
}

Categories