Danson Wachira is a certified Trainer in Computer Science, Information Technology and related studies.
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.
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
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;
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:
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.
The watches window should look like shown below:
Now, click the Step Into icon on the toolbar, alternatively, you can press F7 key on the keyboard.
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.
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.
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.
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.
Other related hubs...
- Programming In Java NetBeans - A Step By Step Tutorial For Beginners: Lesson 22
The Java substring() method is used to extract a given part of string from another string. In this article, you shall learn how to use Java substring() method with strings and how to combine the method with other Java methods such a indexOf() to ...
- Programming In Java NetBeans - A Step By Step Tutorial For Beginners: Lesson 30
Inheritance is one of the difficult concepts to grasp in Object Oriented Programming. Java programming language does provide inheritance where child classes can inherit their parent class. In this article, we shall learn how to implement ...
- Programming In Java NetBeans - A Step By Step Tutorial For Beginners: Lesson 18
Array lists in Java are dynamic structures for store of values, that means values can be added or be removed from the list. We do not even need to declare the size of the list but values can be accessed by their index positions.
- Programming In Java NetBeans - A Step By Step Tutorial For Beginners: Lesson 28
Java constructors and Java setter methods are very useful when used in Java classes. They not only make the debugging of class code simpler but they also add functionality to Java classes. In this article, we shall learn how to create Java ....
Want to make money online?
Danson Wachira (author) from Nairobi, Kenya on November 29, 2012:
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.