Variables, Data Type and Arithmetic Operations


Compilation Errors


Error Message name:

	error: cannot find symbol
	symbol: {variable name}

Example:

	int num = 0;
	System.out.println("The output is" +Num);

This code would give the following error message:

	Error: cannot find symbol
	symbol: variable num

Cause: You have used a variable name that has not being declared.

Solution: Try to find out if you have actually declared and initialized the said variable. Please note that Java is case sensitive, and treats upper and lower case letters differently.


Error Message Name:

	Error: cannot find symbol 

Example:

	int length = 5;
	int width = 6;
	int area = (length) (width);

This would give the following error message:

	Error: cannot find symbol
	symbol : class length

Cause: You are using an operation that is not recognized by the Java Compiler. Although we understand that (a)(b)=a*b, Java does not know that (a)(b) implies multiplication. Instead, it thinks that you are trying to cast width as class type length.

Solution: Add a multiplication sign between the two variables you are trying to multiply. For the above example, we would have:

int area = (length)*(width);

Error Message Name:

	Error: ';' expected

Example:

	System.out.println(Hello World)

Cause: A semicolon is missing at the end of a line.

Solution: Put a semicolon at the end of the line.


Error Message Name:

	Error: ';' expected

Example:

	( 3x + Math.pow(2) ) );

Cause: The numbers of open and close brackets do not match in an expression. There are more close brackets than open brackets.

Solution: Count the number of open and close brackets; make sure they are equal.

Tip: To see which close bracket corresponds to which open bracket, single click right after the close bracket in question. The immediate opening bracket at the start of the highlighted code corresponds to the said close bracket. Below is an illustration:


Error Message Name:

	Error: incompatible type

Example:

	char a = "Hello";

Cause: When declaring and initializing a variable, the variable type on both sides of the "=" sign must match (or can be converted/casted by Java).

Solution: Ensure that the variable types match. For example, if you are declaring an int, the right hand side of the equal sign must evaluate to a whole number. In the above example, change either to String a = "Hello" or char a = 'h'.


Error Message Name:

	Error: variable {variable name} might not have being initialized

Example:

	int p;
	for (int i = 0; i < 5; i ++)
	{  p++;
	}

This would give the following error message:

	Error: variable p might not have been initialized

Cause: You are calling a variable without initializing it or assigning it with a starting value. For primitive types (int, double, float, char and Boolean), you must assign an initial value to a variable before using it.

Solution: Initialize {variable name} with a starting value. A sensible solution for this particular example would be:

	int p = 0; 
	for (int i = 0; i < 5; i ++)
	{  p++;
	}