Before we can debug a program in NetBeans, we need to set a breakpoint . A breakpoint
is a place where the program will stop executing. After the breakpoint, we can debug the
program line by line. In order to set a breakpoint, click on the number of the line. In our
case, we will set a breakpoint at Line 8. After we set a breakpoint, we will see a red circle at
the line of the breakpoint. Note that we can only set a breakpoint at a line that performs
an operation, that is, Line 8 is the first line where we can set a breakpoint. We can also set
a method breakpoint at Line 5. We will then see a red triangle instead of a circle. This is
called a method breakpoint and the program is interrupted for debugging before the method
In order to debug the program, we will select Debug and then Debug Main Project
from the menu. We will see Line 8 marked in green. This is the line that is currently been
executed. We will press F8 to move to the next line. Next, we will press F8 two more times
and enter the temperature in Celsius. Now we are at Line 11. We will press F8 again to
move to Line 12. We will hover with the mouse over the variable c and we will see that c is
equal to 1.0. However, we expected that c will be equal to 5 =1 . 80. We have isolated the
error. We will press the square red button at the bottom of the screen to stop the debugger.
Our error is due to integer division .
The result of dividing two integers is always an integer. The result is always rounded
down to the lowest number. For example, 8/9 is equal to 0.89, which is rounded down
to 0. In other words, Java calculates 8/9 as 0. If we want the result to be a double
and not an integer, we need to convert one of the two arguments to a double before
performing the operation.
There are two popular ways to convert an integer to a double. The first way is to write
(double) before the integer. This is called a casting operation and it converts the integer
to a double. In our example, we can fix our program by rewriting Line 11 as follows.
c=9/(( double )5);
In this case the number 5 is converted to a double. Alternatively, we can simply divide
9 by 5.0 and write the following code.
Note that when we divide two variables, we cannot simply put .0 at the end of the
variable. We need to either cast the variable to a double or multiply it by 1.0. For example,
Line 11 can also be rewritten as follows.
c=(9 ∗ 1.0)/5;
The result of an arithmetic operation that takes as input two integers is always an
integer. However, an operation that takes as input an integer and a double will return a
double. This is why multiplying by 1.0 is a common way of converting an integer to a double.
Correct Line 11 and run the program again. Now it should be working. We have learned
several valuable lessons. First, just because the program works on one example does not
mean that it is correct. We need to run multiple tests to convince ourselves that the pro-
gram is working. Even then, we can never be 100% sure that the program works. However,
performing multiple tests can give us some confidence in the correctness of the program.
When we find a test case for which the program fails, we need to debug the program line
by line and isolate the error. Once the error is isolated, fixing it is not that dicult. It
is common for a novice programmer to spend a lot of time debugging. As you get more
experienced, the debugging time will decrease.
Note that Lines 11 and 12 can be merged into the following single line.