Updated date:

Programming in Java Netbeans - A Step by Step Tutorial for Beginners: Lesson 33

Danson Wachira is a certified Trainer in Computer Science, Information Technology and related studies.

Table of Contents

<< Lesson 32 | Lesson 34 >>

Lesson 33: How to handle logical errors in Java

As we learnt in Lesson 31, logical errors usually results from poor understanding of the program specification and inaccurate translation of the program algorithm. This type of errors can be difficult and tricky to detect and isolate because unlike syntax errors that are underlined by wavy red lines in Java NetBeans, logical errors are not marked in any way and do not stop the program from executing but when the program run, the results are not what would be expected.

As a beginner programmer, you might have encountered logical errors unknowingly especially because the program usually run without throwing any Exception but if you could have checked the output, most likely they were unexpected. Logical errors can be costly when they occur undetected in a program and a programmer can spend lengthy time trying to locate them.

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-33

How to detect, isolate and correct logical errors in Java

In this article, you shall learn how to isolate and correct logical errors using some of the in-built tools found in Java NetBeans. We are going to write a program code and force a logical error.

The program should execute normally but the output will not be as expected. We’ll later use Java NetBeans in-built tools to trace the logical error and correct it. Create a new Java class, type or copy and paste the following code.

This program is supposed to count how many times letter "g" occurs in the word "Debugging". However, when you run the program the output display: "Letter g was found 0 times" so, what could be wrong in the program?

Just to jog your mind, can you spot the logical error is in the program? Probably not but we’ll find out in a minute.

Java program with a logical error

package handleerror;
import javax.swing.JOptionPane;
public class LogicalErrors {
      public static void main(String[] args) {
          int letter_counter = 0;
          String myword = "Debugging";
          String letter = "";
       
          for(int i = 0; i < myword.length(); i++){
             letter = myword.substring( 1,1 );
             if(letter.equals("g")){
                 letter_counter++;
                 
             }
          }
          JOptionPane.showMessageDialog(null,"Letter g was found "+letter_counter+" times");
      }
}
Program output with logical error

Program output with logical error

It is very clear that the program above has an error because the output is not what we expected, however, the program run without throwing Exceptions. This is the hardest part in dealing with logical errors; you don’t have any clue of what could be happening.

Fortunately, Java NetBeans has some in-built tools to help programmers to trace such errors. To start with, we are going to use a tool called Breakpoint at the start of executable statements.

Breakpoint just does as its name suggest; it set a breakpoint in the code to stop execution of the program at that point.

Click on the margins of the code window to add a new Breakpoint at the start of executable statements, in this case at the statement: int letter_counter = 0;

Using Breakpoints in Java

Using Breakpoints in Java

On the Java NetBeans menu bar, Click Debug >> Debug Main Project. (If you have several classes in one project then Right-Click the specific class and select Debug file) Java NetBeans will jump to the Breakpoint and stop code execution at that point. Notice the following new toolbar:

Debugging session toolbar in Java

Debugging session toolbar in Java

The first three buttons on the toolbar allows the programmer to stop the debugging session, pause, and continue the session respectively.

The next five buttons allows the programmer to step into code, step over code, step in or out of code, or jump to the cursor respectively. Alternatively, you can press F5 key to continue. Having Breakpoint in the code does not interfere with the execution of the program in any way.

In the above code, we have a FOR loop that involves three variables; i, letter and letter_counter. We would have expected the variable letter_counter to have a three (3) at the end of the loop as the word “Debugging” has three letters “g” but that is not the case.

What we need to do is to execute the program step by step and keep checking if the loop is working properly and whether actually the letter_counter is being incremented.

On the Java NetBeans menu bar, we are going to add three Watch windows for these three variables. Click on Debug >> New Watch enter variable i as Watch Expression. Repeat this step for the other two variables; letter and letter_counter.

New Watch dialog box

New Watch dialog box

The watches window should look like shown below:

Java debugging Watches window

Java debugging Watches window

Now, click the Step Into icon on the toolbar, alternatively, you can press F7 key on the keyboard.

Showing Step Into icon on the toolbar

Showing Step Into icon on the toolbar

Every time you click on the Step Into icon or press F7 key once, the program execute the next line. Keep pressing the icon or F7 until you enter into the loop. The FOR loop will execute step by step and as it does so keep checking what the variable letter_counter has in every instance of the loop.

The value of variable letter is still empty

The value of variable letter is still empty

Notice on the image above that as the variable i move to value 1 we expect the variable letter to have a string but as you can see on the image, the value of variable letter is still empty. That means the variable counting the number of times i.e. letter_counter cannot move either and that’s why it is still reading a value of zero. That can only mean one thing; that the variable letter_counter is not being incremented and so the following statement has a logical error:

letter = myword.substring(1,1);

Stop the debugging session using the Stop icon.

Showing Stop icon on the toolbar

Showing Stop icon on the toolbar

Change the code with substring() method to this:

letter = myword.substring( i, i + 1 );

Start the debugging session again by pressing the Step into icon. Keep pressing this icon or F7 key like we did before until you finish code execution. This time you will see the values in the variables changing and at the end, you should be able to get the correct output.

The value of variable letter now has string "D"

The value of variable letter now has string "D"

Program output correct result

Program output correct result

So, we have now traced and corrected the logical error that was causing wrong program output. Use of Java in-built tools for tracing and correcting errors such as Breakpoints and Watches can assist a programmer in tracing difficult and tricky errors in a program. In the next lesson, we shall look at how to read a text file in Java.

<< Lesson 32 | Lesson 34 >>

Other related hubs...

Want to make money online?

Comments

Danson Wachira (author) from Nairobi, Kenya on November 29, 2012:

Hi teaches12345,

Always appreciate that you find time to come here, read and comment on my hubs. Thanks a lot.

Dianna Mendez on November 23, 2012:

Yet another great post! Your expertise continues to amaze me. Very informative and detailed hub. Well done.

Related Articles