-4

After runnning my app in emulator to run kind of program it stopped if one of my edit text box is empty which were used in calculation with another edit text boxes to display result in text view boxes and so I must enter value and if I didn't enter it will stop the running program and one of those edit text (editText1) get it value from another activity page so I must go there to put number and I want to use this editText1 "sometimes" without going to that activity

  final EditText editText1 = (EditText)findViewById(R.id.editText1);
  final EditText editText2 = (EditText) findViewById(R.id.editText2);
  final EditText editText3 = (EditText) findViewById(R.id.editText3);

 double thick = Double.valueOf(editText1.getText().toString());
 double width = Double.valueOf(editText2.getText().toString());
 double length =  Double.valueOf(editText3.getText().toString());
 // this is the equation drive that values of previous double edit text
  double Weight = thick * width * length *0.9050 / 1000000 ;
  double Winder_output = thick * width * 0.905 * 60 / 1000000;
  // i displayed this results in Text View
  textView1.setText((String.format("%.2f",Weight)+" KG"));
  textView2.setText((String.format("%.2f", Winder_output)+"KG/HR"));
Kashif Faraz Shamsi
  • 513
  • 1
  • 7
  • 21
Wael Youssef
  • 49
  • 1
  • 10

2 Answers2

0

When edittext empty, it return string "", you should check

if(edittext.getText() != "")

then you can convert to double.

Henry Hieu
  • 19
  • 2
0

Use "if" to check field is empty or not

 if (editText1.getText().toString().trim().equals("")) {
    Toast.makeText(this, "Enter value",Toast.LENGTH_SHORT).show();
     return;
                }

Your code should look like this

      final EditText editText1 = (EditText)findViewById(R.id.editText1);
      final EditText editText2 = (EditText) findViewById(R.id.editText2);
      final EditText editText3 = (EditText) findViewById(R.id.editText3);

     if (editText1.getText().toString().trim().equals("")) {
        Toast.makeText(this, "Enter thick",Toast.LENGTH_SHORT).show();
         return;
                    }   
     if (editText2.getText().toString().trim().equals("")) {
        Toast.makeText(this, "Enter width",Toast.LENGTH_SHORT).show();
         return;
                    }
     if (editText3.getText().toString().trim().equals("")) {
        Toast.makeText(this, "Enter length",Toast.LENGTH_SHORT).show();
         return;
                    } 

     double thick = Double.valueOf(editText1.getText().toString());
     double width = Double.valueOf(editText2.getText().toString());
     double length = Double.valueOf(editText3.getText().toString());