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.
Related
I recently started to teach myself some Python and Java. So I tried to write some easy programs but with this snake game I tried I discovered a mysterious problem. Maybe someone of you knows where the error is.
the code:
for(var i = 0; < snake.tail.length;i++){
...
If you need something else to engage this problem please let me know
Julian
you're missing an "i", and java doesn't use var, use int instead:
for(int i = 0; i < snake.tail.length; i++){
In your code, you have the wrong condition construction:
< snake.tail.length
^
|
|
You don't have anything on the left site. Probably you need the i variable here.
i < snake.tail.length
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
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.
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.
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;
}