Skip to content Skip to sidebar Skip to footer

Error: "cannot Invoke String() On The Primitive Type Float " On Quadratic Formula?

I am getting the error 'Cannot invoke String() on the primitive type float' on my first attempt at an android app: This is the code: public class MainActivity extends Activity {

Solution 1:

You cannot call toString (or any method) on any primitive type, such as float. However, String conversion will convert it to a String for you with the + operator anyway.

answer.setText("The Answer is: " + ans);

If you need more control over the display format, you can use a DecimalFormat.

Additionally, this expression doesn't do what you think it does.

(num2 ^ 2)

The ^ operator is a bitwise-XOR in Java, not exponentiation. Use:

num2 * num2

Solution 2:

Try Float.toString(ans) and Float.toString(ans2). You can't perform .toString (or any instance method) on primitives.

Solution 3:

You don't need to do anything. Just leave the code as below:

 answer.setText("The Answer is: " + ans2);

And similar to next one.

You cannot use toString() or any methods on primitive data types.

Remember: Methods are defined inside a class. So to use method, the variable must first be an object.

Edited:

You can also do

 answer.setText("The Answer is: " + String.valueOf(ans2));

Solution 4:

You cannot call toString() on a primtive. You can use String.valueOf(float)

Stringstr = String.valueOf(ans);
String str2 = String.valueOf(ans2);

Also, Java does not perform exponentiation with ^ (it's bitwise xor)

if ((Math.pow(num2, 2)) - (4*num1*num3) < 0){

Post a Comment for "Error: "cannot Invoke String() On The Primitive Type Float " On Quadratic Formula?"