Reputation: 106
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
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