3

textdisplay.setText(total.toString());

When i am trying to revoke String. It gives me error like "Cannot invoke tostring() on the primitive type double". I read similar thread and tried everything but nothing works. Strange part is that this same code works for my friend. Please help.

public void showsign(String sign){
        if(last_button==R.id.plus || last_button==R.id.minus || last_button==R.id.multiply 
            || last_button==R.id.divide){

    }
    else{
        clear_flag = 1;
        Double newNumber = Double.parseDouble(textdisplay.getText().toString());
        if(sign_flag == "" || sign_flag == "="){
            total = newNumber;
            textdisplay.setText(total.toString());

        }
        else if(sign_flag == "+"){
            total = total+newNumber;
            textdisplay.setText(total.toString());
        }
        else if(sign_flag == "-"){
            total = total-newNumber;
            textdisplay.setText(total.tostring());
        }
        else if(sign_flag == "*"){
            total = total*newNumber;
            textdisplay.setText(total.tostring());
        }
        else if(sign_flag == "/"){
            total = total/newNumber;
            textdisplay.setText(total.tostring());  
        }


    }
} 
Neel
  • 2,100
  • 5
  • 24
  • 47
Freshmen_Java
  • 37
  • 1
  • 1
  • 5

7 Answers7

4

Primitive types are not Objects, and as such don't have .toString().

You have to use (for instance) String.valueOf(), ie:

textdisplay.setText(String.valueOf(total)); 

What is more, this:

if(sign_flag == "/")

and similar will not work. Use "/".equals(sign_flag) instead (or sign_flag.equals("/") if sign_flag is guaranteed not to be null).

fge
  • 119,121
  • 33
  • 254
  • 329
2

You can use String.valueOf(double)

but..

Check out DecimalFormat: http://docs.oracle.com/javase/6/docs/api/java/text/DecimalFormat.html

Unlike String.valueOf you can control the amount of decimal digits that you want to see.

rnrneverdies
  • 15,243
  • 9
  • 65
  • 95
0

A primitive type is not an Object therefore it does not have a toString method.

I can't see the declaration of total but I'm pretty sure that it is a primitive. Try using its wrapper. For example if you see something like double total =... change it to Double total =...

The other problem is that if you use == it means "compare the references". If you want to compare Objects (like a String) use the equals method.

Adam Arold
  • 29,285
  • 22
  • 112
  • 207
0

Double is the wrapper class for the double primitive type. IF you do not want to "Auto-box" doubles into an object, then you can always use: String.valueOf(double)

Neel
  • 2,100
  • 5
  • 24
  • 47
0

If you wish to use .toString(), the variable in question must be an Object. Primitives are not objects. To accomplish what you want and what your friend has probably done. You need to use the wrapper class.

Wrapper class: Double

Primitive: double

Patrick J Abare II
  • 1,129
  • 1
  • 10
  • 31
0

Instead of textdisplay.setText(total.toString()); use textdisplay.setText("" + total);

As others have already pointed out toString() method is defined in Object class, which is inherited by all classes by default, therefore toString() method is available for any class you define or available in library. However, primitives like double, int, float, byte etc. are not classes therefore the variables of these types cannot call toString() method.

Sourabh Bhat
  • 1,793
  • 16
  • 21
0

You cannot convert the double like this directly you should use this ....

    double doublevalue = 0;
    String value = String.valueOf(doublevalue);
Naveen Kumar Kuppan
  • 1,424
  • 1
  • 10
  • 12