There are several ways to do this:
- Step through the problematic code "on paper".
Make a table
for the key variables, and use it to keep track of their values
(as well as any outputs) as you "execute" the statements
in the same order that the interpreter would use. This technique --
which is sometimes called manual execution -- is also
useful when attempting to understand code that someone else
has given you.
- Add temporary println statements at various points in the
problematic piece of code (e.g., inside each loop. Use these
println statements to print the values of the key variables
so that you can see how they change over time. Note that you
can also print out the value of an expression (e.g.,
val < 100
)
to see how it changes over time. If you need to print
an array, you can do so using the Arrays.toString()
method.
- Use the DrJava debugger.
You may find it helpful to consult the following documents:
Don't forget that when you use the debugger to step through
a program, you can use the Interactions Pane to print the
values of variables or expressions.
These approaches should help you to isolate the problematic portion(s)
of your code. If you still aren't able to figure out the problem,
write to cs111-staff @ cs . bu . edu with the relevant portion
of your code and whatever insights you were able to obtain
from the debugging process.