az fav
az fav

Reputation: 106

Int cannot be dereferenced Java

    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.

Upvotes: 2

Views: 2665

Answers (1)

Eran
Eran

Reputation: 393841

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));

Upvotes: 9

Related Questions