Home > database >  when working with gettext in android studio, it gives an error, I don’t understand why
when working with gettext in android studio, it gives an error, I don’t understand why

Time:12-22

Pls, help me. I'm new person this program (sorry my englih very bad, i use google translate

    public class MainActivity extends AppCompatActivity {

    private TextView resultat;
    private EditText input_number1, input_number2;
    private Button buttonadd;
    private View add_button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        resultat = findViewById(R.id.resultat);
        input_number1 = findViewById(R.id.input_number1);
        input_number2 = findViewById(R.id.input_number2);
        buttonadd = findViewById(R.id.buttonadd);

        add_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                float num1 = Float.parseFloat(input_number1, getText().toString());
                float num2 = Float.parseFloat(input_number2, getText().toString());
                float res = num1    num2;
                resultat.setText(String.valueOf(res));

            }
        });
    }
}

I searched for solutions on forms but didn't find anything

CodePudding user response:

Update these two lines

float num1 = Float.parseFloat(input_number1.getText().toString());
float num2 = Float.parseFloat(input_number2.getText().toString());

you were adding space with comma separation that was the error.

CodePudding user response:

as far as I know Float.parseFloat only takes one parameter, and that should be a string, you are passing two parameters, you have mistaken '.' with ',' and thats why you have a problem.

        float num1 = Float.parseFloat(input_number1, getText().toString());

and be aware that getText().toString() doesn't work as a standalone, it should always be called after a textView or EditText or something. like this:

input_number1.getText().toString()
  • Related