Showing posts with label Expression. Show all posts
Showing posts with label Expression. Show all posts

Friday, October 30, 2009

Published 10/30/2009 by with 7 comments

illegal initializer

You may see this error for writing wrong expression to initialize an array. See the code below:

int data={42,15,27,20,19};

If you compile this, you will see the following error message:

illegal initializer for int
int data={42,15,27,20,19};
^

Here the array is not declared properly as we have not put [ ] for declaring the array. The correct code is:

int data[ ]={42,15,27,20,19};

or

int[ ] data={42,15,27,20,19};
Read More
    email this       edit
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 0 comment

illegal start of expression

If a method is defined inside another method we may see ‘illegal start of expression’ error message. Remember that, we cannot define a method inside another method; we just can call a method inside another method. Sometimes it occurs just because of improper opening and closing of curly braces ( { or } ).

See the following example code:
public class IllegalStartOfExpression
{
public void methodA()
{
System.out.println("This is method A");

/* methodB is defined inside methodA because we didn’t close methodA before defining methodB*/

public void methodB()
{
System.out.println("This is method B");
}
}
}

Now see the solution:

public class IllegalStartOfExpression
{
public void methodA()
{
System.out.println("This is method A");
}

/* methodA is finished then, methodB is being defined*/
public void methodB()
{
System.out.println("This is method B");
}

}
Read More
    email this       edit
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

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