Showing posts with label Variable. Show all posts
Showing posts with label Variable. Show all posts

Friday, October 30, 2009

Published 10/30/2009 by with 1 comment

incompatible types

1. Sometimes we may get the following error message in an 'if' (selection) structure.

incompatible types
found : int
required: boolean

This is because, we have not specified a boolean expression as the condition of the 'if'; instead we might have written such an expression which is may be int, float, String or other types but not boolean.

See the code below:

int a=10,b=10;

if(a=b)
{
System.out.println("a and b are equal");
}

Check the condition is a=b. Is it a boolean expression? By using a single = (equal sign), value of b is assigned to a which is an integer. That's not correct. We must use == (double equal sign) for comparing two variables for equality.

Correct code is the below:

int a=10,b=10;

if(a==b)
{
System.out.println("a and b are equal");
}


2. Sometimes it happens because of assigning one type of value to a variable of other types. See the code below:

String text='a';
char ch="a";
In this case the error message will be:

incompatible types
found : char
required: java.lang.String
String text='a';
^
incompatible types
found : java.lang.String
required: char
char ch="a";
^
Here we have not assigned correct value for the variables text and ch. In the first line, we have assigned a char value to a String variable. 'a' is a char value because it is written within ' ' (single quotation mark) . In the second line, we have assigned String value "a" to a char type variable. Note that String value is written within " " (double quotation mark). So we tried to assign char value to a String variable and a String value to a char variable. The correct code is:

String text="a";
char ch='a';

If you see this error, check that the correct type of value or variable is assigned.

Read More
    email this       edit

Sunday, May 17, 2009

Published 5/17/2009 by with 2 comments

identifier expected

This error message is shown when the statements are not written in proper place. Most often, mistakenly, we may write the processing task (for example assigning value to a variable; writing a loop etc.) outside of any method. In this situation we may see this type of error message.

Then the solution is to write such statements inside the appropriate methods.

See the example code below:
public class IdentifierExptected
{
int number1,number1,sum;


number1=10; // identifier expected
number2=20; // identifier expected
sum=number1+number2; // identifier expected

/*
The assignment statement must be written inside a method. Variables can also be assigned/ initialized during the declaration.
*/


public void display()
{
System.out.println("Number 1 = "+number1);
System.out.println("Number 2 = "+number2);
System.out.println("Sum = "+sum);
}


}

See the solution now:

public class IdentifierExptectedSolved
{
int number1=10,number2=20,sum;


public void sum()
{
sum=number1+number2;
}


public void display()
{
System.out.println("Number 1 = "+number1);
System.out.println("Number 2 = "+number2);
System.out.println("Sum = "+sum);
}



}
Read More
    email this       edit

Sunday, November 9, 2008

Published 11/09/2008 by with 2 comments

possible loss of precision

You may see the error message 'possible loss of precision' for any of the following reasons:

1) You are trying to assign a fractional number into a float variable(why not?). Actually Java compiler considers the fractional number as double by default. You need to explicitly specify the fractional number as float by giving the character 'f' or 'F' after the the number, like float number = 65.37F;

2) Or you are trying to assign the value of a double variable into a float variable. Memory size of double is 8 byte and that of float is 4 byte. If you assign a double value into a float value data may be lost. So be sure about the data type. If you are sure you want to assign the double into float, you need to cast it appropriately, like float floatVariable = (float)doubleVariable;

An example is given below:

public class PossibleLossOfPrecision
{
public static void main(String args[])
{

float floatNumber1=65.37; // error
double doubleNumber=65.37;
float floatNumber2=doubleNumber; // error

System.out.println(floatNumber1);
System.out.println(doubleNumber);
System.out.println(floatNumber2);

}
}


The above code is wrong; write this code as below and the problem will be solved.

public class PossibleLossOfPrecisionSolution
{
public static void main(String args[])
{

float floatNumber1=65.37F; // error solved
double doubleNumber=65.37;
float floatNumber2=(float)doubleNumber; // error solved

System.out.println(floatNumber1);
System.out.println(doubleNumber);
System.out.println(floatNumber2);

}
}
Read More
    email this       edit

Tuesday, November 4, 2008

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

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