What is an Error in Program?
- Errors are mistakes in a program that prevents it from working condition.
- Three types of error
- Compile Time Error
- Logical Error
- Runtime Error
- This document discusses the compile time and logical errors.
Unresolved compilation problem
Exception in thread "main" java.lang.Error: Unresolved compilation problem
- This happens when the
public class
keyword used other than Java File Name
- Example: Java File Name:
Employee.java
but public class is EmployeeTest
- Solution: File must be renamed as
EmployeeTest.java
class Employee {
private String name;
public Employee(String name) {
this.name = name;
}
public String toString() {
return "Employee [name=" + name + "]";
}
}
public class EmployeeTest {
public static void main(String[] args) {
System.out.println(new Employee("Kumar").toString());
}
}
The method is undefined
- Reason-1: There might be a spelling mistake in the method name.
- Reason-2: The method might not have been created in class.
- Example
class Employee {
private String name;
public Employee(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
public class EmployeeTest {
public static void main(String[] args) {
Employee employee = new Employee("Kumar");
System.out.println(employee.getNaem());
}
}
Syntax error, insert ";" to complete Statement
- Check the Line Number displayed in Error Message
- Add a "semicolon;" at the end of that statement
public class EmployeeTest {
public static void main(String[] args) {
System.out.println("Welcome to Java")
}
}
Syntax error on token ",", ; expected
- Reason - comma(,) was used instead of a semicolon (;)
- Example
public class EmployeeTest {
public static void main(String[] args) {
for (int i = 0, i < 3; i++) {
System.out.println(i);
}
}
}
String literal is not properly closed by a double-quote
- Reason: The starting is not properly closed with a double quote
import java.time.LocalDate;
public class EmployeeTest {
public static void main(String[] args) {
System.out.println("Sample program to display current date: );
System.out.println(LocalDate.now());
}
}
Unreachable Code or Statement
class Employee {
private String name;
public Employee() {}
public Employee(String name) {
this.name = name;
}
public String concatUpper(String message) {
String newStr = message + this.name;
return newStr;
newStr.toUpperCase();
}
}
public class EmployeeTest {
public static void main(String[] args) {
Employee employee = new Employee("Kumar");
System.out.println(employee.concatUpper("Welcome"));
}
}
Logical Mistake: Using Logical Operators
- Write a Java Program for checking the given name has only an alphabets and minimum 3 characters
public class EmployeeTest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String name = input.nextLine();
if (name.matches("[a-z]+") && name.length() < 3) {
System.out.println("Invalid Input");
} else {
System.out.println(name);
}
}
}
Comments
Post a Comment