Int cannot be dereferenced Java

az fav :
    try
    {
        if (expr.contains("+"))
        {
            return (Integer.parseInt(left) + Integer.parseInt(right)).toString();;
        }
        else if (expr.contains("*"))
        {
            return (Integer.valueOf(left) * Integer.valueOf(right)).toString();
        }
        else if (expr.contains("-"))
        {
            return (Integer.parseInt(left) - Integer.parseInt(right)).toString();
        }
        else
        {
            return (Integer.parseInt(left) / Integer.parseInt(right)).toString();
        }
    }
    catch (java.lang.Exception e)
    {
    }

Am trying to do operations on integers,then, convert int to string but am getting int cannot be dereferenced error.

Eran :

Integer.parseInt(left) - Integer.parseInt(right) is an int. An int is a primitive, so it has no methods, therefore you can't call toString() on an int value.

You can use the static Integer.toString() method instead.

For example:

return Integer.toString(Integer.parseInt(left) - Integer.parseInt(right));

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=112322&siteId=1