Showing posts with label Selection. Show all posts
Showing posts with label Selection. 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

Tuesday, November 4, 2008

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