Recover from Errors
Learn about some common bugs and how to avoid them.
In this lesson, we’ll look at some of the common errors that we, as programmers, make and how to watch out for them. So let’s begin!
Bugs
Following proper syntax and sequence is imperative while writing code. But we all make mistakes, and running into errors while we code is not uncommon. The unintended errors and flaws in code and its functionality are called bugs.
The two most common types of bugs are syntax errors and logical errors.
Syntax errors
Syntax errors occur when the code doesn’t conform to the syntax rules of the programming language. These errors prevent the program from running successfully. The code below provides an example of a syntax error in our code.
import java.util.Scanner; public class MyJavaApp { public static void main(String[] args) { Scanner scan = new Scanner(System.in); // Initialize a variable 'fullName' with an empty string String fullName = ""; // Prompt the user to enter their first name and store it in the firstName variable System.out.print("Enter your first name: )"; String firstName = scan.nextLine(); String lastName = ""; // Prompt the user to enter their first name and store it in the firstName variable System.out.print("Enter your last name: "); lastName = scan.nextLine(); // Concatenate the names with a space in between and store the result in 'fullName' fullName = firstName + " " + lastName; // The '+' symbol can be used to combine two strings. // Display a message with the user's full name using the 'fullName' variable System.out.println("My name is "+ fullName); } }
Find the bug
Which line in the code has a syntax mistake?
Line 8
Line 18
Line 9
Line 12
Line 10
If we fix the error, the code will run just fine. ...