Programmer
Programmer

Reputation: 21

why i cannot operate on Edit text int?

hi i wanted to make a simple bmi app for android and as you can see error happens why i can not operate on Edit text as you can see i converted it to int value but still i have this problem.

 package my.myapp;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;

 public class WelcomeActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome_activity);
        final EditText eText = (EditText) findViewById(R.id.editText);
        final EditText eText2=(EditText) findViewById(R.id.editText2);
        final TextView tView = (TextView) findViewById(R.id.textView);
        final Button butt = (Button) findViewById(R.id.button);
        View.OnClickListener listen = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String E1value =eText.getText().toString();
                int E1value2=Integer.parseInt(E1value);
                String E2value=eText2.getText().toString();
                int E2value2=Integer.parseInt(E2value);
                int answer=(E1value2)/(E2value2/10)^2;
                tView.setText(answer);

                            }
        };
        butt.setOnClickListener(listen);
    }
 }

Upvotes: 0

Views: 38

Answers (1)

Prajnan Bhuyan
Prajnan Bhuyan

Reputation: 425

There are a few things you can do:

1) setText( ) accepts a string as an argument. So pass answer as

tView.setText(""+answer);

2) Declare E1value2, E2value2 and answer as float or double as E2value2/10 might return 0 if the number entered in the second EditText is smaller than 10

int answer=(E1value2)/(E2value2/10)^2;

So, you'll need to change your code to (I made a few edits of my own to simplify it a little)

float E1value = Float.parseFloat(eText.getText().toString());
float E2value = Float.parseFloat(eText2.getText().toString());
float answer = (E1value) / (E2value / 10);
answer = (int) answer ^ 2;
tView.setText("" + answer);

Also I would like to recommend you to validate the data entered in the EditText before performing the calculations. To make sure they are not empty.

Upvotes: 2

Related Questions