Search code examples
javac++opencvoperatorsjavacv

Trying to convert a bunch of C++ codes into Java - if statement


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


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.