Sunday, November 9, 2008

Published 11/09/2008 by with 1 comment

integer number too large

We know that the maximum value of an int is 2147483647. If we write any whole number more than 2147483647 in our Java program, we may get this error message 'integer number too large' but may be you are trying to store this large number in a long type variable, then what to do? The solution is to put the letter 'l' or 'L' after the number. This happens, because the compiler considers the whole number as int by default and we specify the large number by character 'l' or 'L' as long number. Now the compiler will consider the number as long.

For example, we are trying to compile the following Java code:
public class IntegerNumberTooLarge
{
public static void main(String args[])
{
long number=965478213245;
System.out.println(number);
}
}

We will get 'integer number too large' from the above program. Now write the program as below the problem will be solved.

public class IntegerNumberTooLargeSolution
{
public static void main(String args[])
{
long number=965478213245L;
System.out.println(number);
}
}
Read More
    email this       edit

Tuesday, November 4, 2008

Published 11/04/2008 by with 0 comment

; (semi-colon) expected

Each java statement must ends with ; (semicolon) in Java. If we don’t put ; after any statement we will see ‘;’ expected

Here is an example of this error:
public class SemicolonExpected
{
public static void main(String args[])
{
int x=10,y=10 //no ; (semicolon) is given after this statement
if(x==y)
System.out.println(x +" and "+y+" is equal");
}
}

Here no ; (semicolon) is given after the x,y declaration statement. The correct code is:

public class SemicolonExpectedSolution
{
public static void main(String args[])
{
int x=10,y=10; //; (semicolon) is given now
if(x==y)
System.out.println(x +" and "+y+" is equal");
}
}
Read More
    email this       edit
Published 11/04/2008 by with 0 comment

variable … might not have been initialized

We see “variable might not have been initialized” when we try to use the value of a variable before assigning any value to that variable. Each local variable must be assigned a value before further using it.

Here is an example of this error :
public class VariableMightNotHaveBeenInitialized
{
public static void main(String args[])
{
int a,b,c;
a=10;
c=a+b;
System.out.println("The sum is "+c);
}
}


Here variable ‘b’ is added with variable ‘a’ before assigning any value.

Correct code is:
public class VariableMightNotHaveBeenInitializedSolution
{
public static void main(String args[])
{
int a,b,c;
a=10;
b=20;
c=a+b;
System.out.println("The sum is "+c);
}
}
Read More
    email this       edit
Published 11/04/2008 by with 0 comment

missing method body, or declare abstract

If we put a ; (semicolon) after the method header (i.e., before starting the method body) when we are defining a concrete method (which method has body) then we will see “missing method body, or declare abstract”. Another reason is if we don’t write the keyword ‘abstract’ for an abstract method (a method without body), we’ll also see this message.
To resolve the problem we must remove the ; (semicolon) from the header of the concrete method or if it is an abstract method the keyword ‘abstract’ must be written in the method header.

Here is an example of this error:
public abstract class MissingMethodBodyOrDeclareAbstract
{
public void display(String message);
{
System.out.println(message);
}
public void display();
}

Here, a ; (semicolon) is given after the method header of the first display method (with parameter). And for the second display method, keyword ‘abstract’ is not stated in the header and it doesn’t have any body also. Here it is to be noted that if a class contains an abstract method, the class must also be abstract.

The correct code is:

public abstract class MissingMethodBodyOrDeclareAbstractSolution
{
public void display(String message)
{
System.out.println(message);
}
public abstract void display();
}
Read More
    email this       edit
Published 11/04/2008 by with 0 comment

else without if

Programmers may see message “else without if” when the selection (if ... else if ... else) structure is used. This may happen for any of the following reasons:
Semicolon (;) is given after the last parenthesis of the condition
Error in use of the curly braces ( } ), may be the } is not closed before starting the next else if or the else or } is given more.

Here is an example of this error:
public class ElseWithoutIf
{
public static void main(String args[])
{
int a=10,b=5;
if(a==b);
{
System.out.println(a+" is not equal to "+b);
else
{
System.out.println(a+" is equal "+b);
}
}
}


Here we have given ; (semicolon) at the last of the first condition, it’s an error. Again, we haven’t closed the } of the if i.e., else is written within the ‘ if’. So we need to remove the ; and put another } before the else.

Now the correct code is:
public class ElseWithoutIfSolution
{
public static void main(String args[])
{
int a=10,b=5;
if(a==b)
{
System.out.println(a+" is not equal to "+b);
}
else
{
System.out.println(a+" is equal "+b);
}
}
}
Read More
    email this       edit
Published 11/04/2008 by with 0 comment

cannot find symbol / cannot resolve symbol

“cannot find symbol” or “cannot resolve symbol” is the most common error message which the programmers get from the compiler. However this error message may mean that the variable/method/class/object used are not declared/defined properly. In most cases, this happens because:

· The variable/object is not declared or the spelling of variable/object declaration & later use of that variable/object is not same.

· The method is not defined or the spelling of the method definition and method calling is not same or the number and/or type of parameters are wrongly given when calling the method or the method where it is defined is not imported properly (in case of class library methods, the package where the method is defined is not imported)

· If the message is shown for a class, the package of that class might not have been imported or the spelling is wrong.

The solution of that problem is to check the spelling at first, Java programmers see this message most often just because of the wrong spelling( be careful about the case also, as Java is a case sensitive language). If the spelling is correct check whether the required package is imported.

Here is an example of this error :

public class CannotFindSymbol
{
public static void main(String args[])
{
int quantity;
Scanner input=new Scanner(System.in);
System.out.print(“Enter the quantity: ”);
quntity=imput.nextInteger();
System.out.println(“Your number is ”+quantity);
}
}


In the above code, there are many lines where we will get this error message.

At first it will say Scanner cannot be resolved/found because the package of this class java.util is not imported.

Also problem in ‘quntity’ as the variable is declared as ‘quantity’

Error in ‘imput’ as the object is created as ‘input’

If you correct all the errors it will show error message for nextIntger as the actual method name is nextInt in class Scanner

Now the correct code is:

import java.util.*;
public class CannotFindSymbolSolution
{
public static void main(String args[])
{
int quantity;
Scanner input=new Scanner(System.in);
System.out.print("Enter the quantity: ");
quantity=input.nextInt();
System.out.println("Your number is "+quantity);
}
}
Read More
    email this       edit