简体   繁体   中英

Calculator-Error when try to sum an integer with an empty field

I've made a calculator that sums two numbers come from the user.num1 and num2 are the edittexts provide to receive the numbers from user.Result is a textview that shows the result on the screen.It correctly make the sum when inputs are two integers but if one of them are null for instance(5+null) or (null+2) application gives error and shuts down itself.Where's my mistake?Here's what i tried

 public class MainActivity extends AppCompatActivity {

    EditText num1;
    EditText num2;
    TextView result;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        num1=(EditText)findViewById(R.id.number1);
        num2=(EditText)findViewById(R.id.number2);
        result=(TextView) findViewById(R.id.result);
    }
   public void plus(View view) {
       int a = Integer.parseInt(num1.getText().toString());
       int b = Integer.parseInt(num2.getText().toString());
           if(num1.getText().toString()==null)
           {
               a=0;
           }
           if(num2.getText().toString()==null)
           {
                b=0;
        }
       int sum2 = a + b;
       result.setText(" " + sum2);
   }

Every time that you parse a string to a number you must use try/catch. This will catch any parsing error including the empty string:

public void plus(View view) {
    int a = 0;
    int b = 0;

    try {
        a = Integer.parseInt(num1.getText().toString().trim());
    } catch (NumberFormatException e) { }

    try {
        b = Integer.parseInt(num2.getText().toString().trim());
    } catch (NumberFormatException e) { }

    int sum2 = a + b;

    result.setText("" + sum2);
}

I think you need to check wether your EditText has data in it before trying to parse the string into an int.

What you are doing in the code is wrong because you call the getText() method without any value inside the EditText, thus trying to parse the value “null” to an int. Trying to parse non numeric values to an int throws an exception, which in turn, causes the app to crash.

Ps: you could probably see the exception thrown in the Logcat window in the bottom of Android Studio.

Hope my answer helped you.

public void plus(View view) {

   try{

      int a = Integer.parseInt(num1.getText().toString());
      int b = Integer.parseInt(num2.getText().toString());

   }

   catch(NumberFormatterException e){

       if(num1.getText().toString()==null)
       {
           a=0;
       }
       if(num2.getText().toString()==null)
       {
            b=0;
      }
  }

   int sum2 = a + b;
   result.setText(" " + sum2);

   }

try it :

public class MainActivity extends AppCompatActivity {
EditText num1;
EditText num2;
TextView result;
Integer a=0,b=0;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    num1=(EditText)findViewById(R.id.number1);
    num2=(EditText)findViewById(R.id.number2);
    result=(TextView) findViewById(R.id.result);
   }
   public void plus(View view) {

  if(num1.getText().toString()!=null||num1.getText().toString().equal("")){
    a = Integer.parseInt(num1.getText().toString());

       }else {
           a=0;
             }
     if(num2.getText().toString()!=null||num2.getText().toString()equal("")){
       b = Integer.parseInt(num2.getText().toString());

        }else {
             b=0;
              }
     result.setText(String.valueOf(a+b) );     
  }

Also go to Your EditText in XML and set InputType in EditText ->number its will work as well

You can initialize variables a and b fist with 0 values and then check null for num1 and num2. Refer below code:

    public void plus(View view) {
        int a = 0;
        int b = 0;

        if (num1 != null && num1.getText() != null && num1.getText().toString() != "") {
            a = Integer.parseInt(num1.getText().toString());
        }

        if (num2 != null && num2.getText() != null && num2.getText().toString() != "") {
            b = Integer.parseInt(num2.getText().toString());
        }

        int sum2 = a + b;
        result.setText(" " + sum2);
    }
        public void onClick(View view) {
            int n1 = 0, n2 = 0;
            try {
                n1 = Integer.parseInt(num1.getText().toString());
            }
            catch(NumberFormatException e) {
                if (num1.getText().toString() == null) {

                    n1 = 0;
                }
            }
            try {
                n2 = Integer.parseInt(num2.getText().toString());
            }
            catch(NumberFormatException e) {
                if (num2.getText().toString() == null) {
                    n2 = 0;
                }
            }
            int sum = n1 + n2;
            result.setText("Answer : " + String.valueOf(sum));

        }

This is how you can handle the exception, if there is any empty field. Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM