2

Trying to make a calculator, here's the button method when the line with the * is ran it comes up with the error "operator + cannot be applied to java.lang.charsequence". If any one know's a way around this please help.

    public void Button_Click(View view) {
    TextView result = (TextView) findViewById(R.id.result);
    Button b = (Button)view;
    if ((result.getText() == "0")||(operation_pressed))
        result.setText("");
    operation_pressed = false;

    if (b.getText() == ".") {
        result.getText() = result.getText() + b.getText()); ***
    }
}    

Thanks

Kyle Wilko
  • 23
  • 3

1 Answers1

0

In order to convert a charsequence to a String apply .toString() to the CharSequence Object and you should be fine to use + operators:

[https://docs.oracle.com/javase/7/docs/api/java/lang/CharSequence.html][1]

Also it looks like you have a syntax error:

   result.getText() = result.getText() + b.getText()); *** the last ) is too much

Furthermore you cannot write into getText() .. Getters (indicated by getXXX()) should be used to get values while Setters (indicated by setXXX()) should be used to store values into an object.

result.setText(result.getText().toString() + b.getText().toString())

foho
  • 28
  • 5
  • thanks, you sort of helped but now I get the error operator + cannot be applied to 'void java.lang.string this has left me as confused as ever with my problem. any more help would be great, thanks. – Kyle Wilko Nov 12 '14 at 05:23
  • never mind I just declared a string that was result_text then instead of declaring result's text as a variable I used result.setText result.setText(result_text + b.getText().toString()); – Kyle Wilko Nov 12 '14 at 05:39
  • glad the advice could help you! Nevertheless I'm wondering why you had the problem. since both toString Methods should return a string! – foho Nov 12 '14 at 08:10
  • Thanks and I have no idea why that happened... I am using Android Studio (Beta) 0.8.9 so maybe that was the problem, anyway thanks. – Kyle Wilko Nov 13 '14 at 06:09