Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Int cannot be dereferenced Java

Tags:

java

string

int

    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.

like image 623
az fav Avatar asked Mar 12 '26 03:03

az fav


1 Answers

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));
like image 62
Eran Avatar answered Mar 14 '26 15:03

Eran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!