3. Debugging¶
3.1. Objectives¶
- Recognize and correct syntax errors in a Java program.
3.2. Time Goal¶
- 25 minutes on this section
3.3. Key Terms¶
- syntax error
- A mistake in the source code that violates the rules of the language
- compile time error
- An error that is found during the compilation. These are also called syntax errors.
- logic error
- mistake in the program that causes incorrect behavior (although it violates no language rules)
- bug
- An error in a program.
- debugging
- The process of finding and removing errors.
3.4. Exercises¶
Note
This part of the lab will give you some practice in reading and interpreting Java. As you work through each question, pay particular attention to the message produced, and in some cases, a single error will cause several other errors.
Note
Remember that the Java source file has to be translated into a class file before it can be run.
The compiler tries to make sense of your code, but if your code is incorrect,
which means it has syntax errors, you will see error messages displayed below the code.
A syntax error is an error in the specification of the program. An example of a syntax error is if
the code has a open curly brace {
, but no close curly brace }
.
The error messages will tell the line number that the compiler found the error and the type of error. The error messages are not always easy to understand and sometimes the actual error is before the line that the complier says is the problem.
Note
Click on the Run button below to try and run the following code. Look for an error message after the code. This is called a compile time error because it is an error detected by the compiler.
What is wrong? Can you fix it? The error message will tell you the line number
that it thinks is causing the error (SecondClass.java:5: error: unclosed string literal
).
Check line 5 to make sure that everything looks correct.
One good thing to check is that all {
have a matching }
and all (
have a matching )
and all starting "
have a ending "
as well.
Note
Note
Notice that the compiler claims that there are 3 errors,
but all the errors are caused by the same problem (the missing end "
).
Fix the code and run it again.
Note
Click on the Run button below to try and run the following code.
Look for an error message after the code. What is wrong this time?
Can you fix it? One good thing to check is that all {
have a matching }
and all (
have a matching )
and all starting "
have a ending "
as well.
Note
Note
Click on the Run button below to try and run the following code.
Look for an error message after the code. What is wrong this time?
Can you fix it? One good thing to check is that all {
have a matching }
and all (
have a matching )
and all starting "
have a ending "
as well.
Note
Note
Use the code editor below to run your java program in the web browser windows. Or you can click on the editor link here to open the code editor in a new window. You can have your partner work along in a separate instance of the code editor if that helps. Note that you can use the pencil icon to edit a file again if you need to go back and make changes after you’ve used the triangular “run” button.
Note
What is the name of the source file in this program?
Hello.java:1: error: class ello is public, should be declared in a file named ello.java public class ello{ ^ 1 error
- Correct!
ello.java:1: error: class Hello is public, should be declared in a file named Hello.java public class Hello{ ^ 1 error
- It looks like you changed the name of the file instead of the class inside the file. Please try again.
Hello.java:3: error: cannot find symbol System.out.printl("Hello World!"); ^ symbol: method printl(String) location: variable out of type PrintStream 1 error
- If you're getting this message, please ask your TA for help to get you back on track.
Error: Main method is not static in class Hello, please define the main method as: public static void main(String[] args)
- If you're getting this message, please ask your TA for help to get you back on track.
Q-66: Class name different from file name.
Delete the beginning ‘H’ from the name of the class (so the first
non-comment line is public class ello
) and save the program.
Now compile and run your program. By clicking the triangle icon. What error message do you get during the compile?
- The compiler was run in silent error mode.
- Incorrect. Try again.
- The code is logically correct.
- Try again
- The code is syntactically correct.
- Correct
- The class file was not executed.
- Incorrect--the class file was executed. Try again.
- The class was marked as public.
- While this is true, it isn't relevant. Try again.
Q-67: Misspelling inside a string literal.
Hello.java:3: error: unclosed string literal System.out.println("Hello World!); ^
- Correct!
Hello.java:3: error: ';' expected System.out.println("Hello World!);
- Incorrect--this is a bogus error message. Try again.
Hello.java:5: error: reached end of file while parsing } ^
- Incorrect--you may need to fix a missing close brace before you try again.
Hello.java:3: error: ';' expected System.out.println("Hello World!") ^ 1 error
- Incorrect--you may need to fix a missing semicolon and try again.
Q-68: No ending quote mark for a string literal.
- The errors messages were identical
- The error messages were different
- The error occured on line 3
- The error occured on line 5
- This is a logic error
Q-69: No beginning quote mark for a string literal.