Question

i was trying to write some C++ codes into java, now i have writter following code into java but it is throwing errors!

if(ShapeNotFound && xd*yd - nPixel[k] < xd+yd)              // Condition for RECTANGLE
{

    System.out.print("\n      "+in+"  \t     Rectangle \n");
    fileWriter3.write("\n      "+in+"  \t     Rectangle \n");
    Shape[k] = 2;
    ShapeNotFound = 0;

}

I am getting following error :

The operator && is undefined for the argument type(s) int, boolean

Please help, tell me how to write the above if condition correctly in java

Was it helpful?

Solution

C and C++ both assume that for integers 0 is false and all other values are true.

Java does not make the same assumption so you need to add a check for int!=0 into the expression i.e.:

if((ShapeNotFound!=0) && (xd*yd - nPixel[k] < xd+yd))    

Or alternatively your ShapeNotFound variable should be of type boolean not int.

It would be worth converting variable names etc to Java style guidelines as well.

OTHER TIPS

Java can not convert int into boolean automatically.

It looks like ShapeNotFound is an integer, but you're implicitly treating it like a boolean (true or false). Java only likes genuinely boolean expressions, so you'll need to change the condition to something like this:

if (ShapeNotFound != 0 && xd*yd - nPixel[k] < xd+yd)

For readability, I'd suggest putting some brackets round each part of the condition. That's an issue of personal preference though.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top