Monday, August 18, 2014

NETBEANS Java Basics 113 – Logic Errors

---
Java Basics 113– Logic Errors

STEPS

1) CREATE NEW PROJECT

Project Name: errorhandling2
Main Class Name: errorhandling.errorChecking2
Delete all comments and unnecessary codes, leaving only the following codes.
package errorhandling2;
public class errorChecking2 {
    public static void main(String[] args) {
        // TODO code application logic here
    }
   
}

2) Edit codes.

2-1) Insert the codes for main method as follows.
package errorhandling2;
public class errorChecking2 {
    public static void main(String[] args) {
        int LetterCount = 0;
        String check_word = "Debugging";
        String single_letter = "";
        int i;
        for (i = 0; i < check_word.length(); i++) {
            single_letter = check_word.substring(1, 1);
            if (single_letter.equals("g")) {
                LetterCount++;
            }
        }
        System.out.println("G was found " + LetterCount + " times.");
    }
   
}
2-2) Run.
Problem: The word “Debugging” contains two “g”s but the program outputs that “G was found 0 times.”

3) Add Break Point.

3-1) Click the left margin of the code editor window to mark the Break Point.
3-2) Go to menu bar, select Debug/Debug Project.
3-3) Click Step Into button.
3-4) Watch the change in the value of variables.
3-5) Stop the debugging.
3-6) Edit your codes as follows.
package errorhandling2;
public class errorChecking2 {
    public static void main(String[] args) {
        int LetterCount = 0;
        String check_word = "Debugging";
        String single_letter = "";
        int i;
        for (i = 0; i < check_word.length(); i++) {
            single_letter = check_word.substring(i, i+1);
            if (single_letter.equals("g")) {
                LetterCount++;
            }
        }
        System.out.println("G was found " + LetterCount + " times.");
    }
   
}
3-7) Observe the Variables in Debug Window.

REFERENCE

---

No comments:

Post a Comment