-3

here is my code but when i pressed the button without give the number app stopped whole day now please tell me what can i do for this The code is given below

public class MainActivity extends Activity { TextView result;

EditText editText1, editText2;
Button btnadd;


@Override

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






             editText1 = (EditText) findViewById(R.id.editText1);
             editText2 = (EditText) findViewById(R.id.editText2);
             btnadd = (Button) findViewById(R.id.btnadd);
             result = (TextView) findViewById(R.id.textviewresult);


             btnadd.setOnClickListener(new View.OnClickListener()




             {
                 @Override
                 public void onClick(View view) {








                     int a = Integer.parseInt(editText1.getText().toString());
                     int b = Integer.parseInt(editText2.getText().toString());

                     int add = a + b;



                     result.setText("Answer: " + String.valueOf(add));


             }
             });
Zoe
  • 27,060
  • 21
  • 118
  • 148

3 Answers3

0

Try this, also restrict the edit texts to allow only numbers..

`````````````````
int a = 0;
int b = 0;

if(!editText1.getText().toString().trim().equalsIgnoreCase(""))
    a = Integer.parseInt(editText1.getText().toString());
if(!editText2.getText().toString().trim().equalsIgnoreCase(""))
    b = Integer.parseInt(editText2.getText().toString());

int add = a + b;
result.setText("Answer: " + String.valueOf(add));
```````````````````````
deepThought
  • 699
  • 4
  • 26
  • modified the answer – deepThought Apr 16 '19 at 17:46
  • can I use intent in main activity and show the result in another activity – imtiajul islam Apr 18 '19 at 17:15
  • You can use Intent to pass data between activities, if you can tell me the exact thing you are trying to do, I can add more points. – deepThought Apr 18 '19 at 17:16
  • when I give the number in mainactivity and press add button then the result I mean Textview should be shown in second activity thats all what should I do for that sir – imtiajul islam Apr 18 '19 at 17:20
  • Just for your practice, create one more activity with a result textview in it, Pass addition result from first activity to second activity using this . `intent.putExtra("intResult", intValue);' , then receive the result in second activity using this : 'int result = mIntent.getIntExtra("intResult", 0);' .. display the value of result – deepThought Apr 18 '19 at 18:40
  • sir one more question how i take decimal numbers in this code – imtiajul islam Apr 19 '19 at 16:14
  • android:digits="0123456789" android:inputType="number" , Add this in layout file if you want only positive numbers. – deepThought Apr 20 '19 at 14:31
0

The problem is Integer.parseInt cannot parse an empty string

error message

so I checked this

Check if EditText is empty.

and I modified your code like this

public class MainActivity extends AppCompatActivity {

TextView result;
EditText editText1, editText2;
Button btnadd;

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

    editText1 = (EditText) findViewById(R.id.editText1);
    editText2 = (EditText) findViewById(R.id.editText2);
    btnadd = (Button) findViewById(R.id.btnadd);
    result = (TextView) findViewById(R.id.textviewresult);


    btnadd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            int a;
            int b;
            if (editText1.getText().toString().matches("")) {

                a=0;
            } else{
             a = Integer.parseInt(editText1.getText().toString());
            }

            if (editText2.getText().toString().matches("")) {
                b = 0;
            }else {
                b = Integer.parseInt(editText2.getText().toString());
            }

            int add = a + b;


            result.setText("Answer: " + String.valueOf(add));


        }

    });
}

}

0
private void intiView() {
    edt1 = findViewById(R.id.edt1);
    edt2 = findViewById(R.id.edt2);
    btnSum = findViewById(R.id.btnSum);
    txtAns=findViewById(R.id.txtAns);
    btnSum.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!edt1.getText().toString().isEmpty() && !edt2.getText().toString().isEmpty()) {
                sum = Integer.parseInt(edt1.getText().toString()) + Integer.parseInt(edt2.getText().toString());
                Toast.makeText(Main2Activity.this, "Sum of two nos: " + sum, Toast.LENGTH_LONG).show();
                txtAns.setText(""+sum);
            } else if (edt1.getText().toString().isEmpty()) {
                edt1.setError("Please enter no1 ");
            } else if (edt2.getText().toString().isEmpty()) {
                edt2.setError("Please enter no2 ");

            }
        }
    });
}
Tom
  • 1
  • 2