Jeed
Jeed

Reputation: 9

How Ignore Empty EditText?

I have 6 Edit Text that if the user inputs all of them will do some calculations, I want to ignore Empty Edit Text so that my app will not crashes if there is no inputs, I find way but it's painful and time consuming is to put each possibility's for edit text. For Addition I find simple why just check if the Edit Text is Empty put value 0,

Edittext.SetText("0");

So I can perform Addition if only for example 3 inputs given like ed1=1, ed=1, ed3=1

The edTotal = 1 + 1 + 1 + 0 + 0 + 0 = 3;

my problems is with other operations if division or multiply we can't set it to zero it will change the result.

And sorry still learning to code.Thanks for any Help.

Upvotes: 0

Views: 767

Answers (4)

Pranata
Pranata

Reputation: 1

or you can make still your edit text the vale + 0 like Integer.interger ("0" + editText.getText().toString()); }

or input Toast.makeText(Profesional_5x1.this, "No valid values, et least please input two result measurements MUAC, Knee Height, and age", Toast.LENGTH_SHORT).show();

Upvotes: 0

Deep Dave
Deep Dave

Reputation: 157

You can create a method which return int value.

 private int extractInt(EditText editText) {
        return editText.getText().toString().isEmpty() ? 0 : Integer.parseInt(editText.getText().toString());
    }

Which says check if edit text is empty, return 0 or return integer value.

Use created method wherever you need it.

int Total = extractInt(youreditText1) + extractInt(youreditText2) + extractInt(youreditText3) + extractInt(youreditText4);

Upvotes: 0

Alex Grebennikov
Alex Grebennikov

Reputation: 170

I wrote some utilities methods you can use. You will need to remove default value of "0" that you are currently using. This way in case EditText has no Integer value calculation for that EditText will be skipped.

Modify those functions for your needs :)

private void addValueFromEditText(EditText yourEditText) {
    try {
        edTotal += Integer.valueOf(yourEditText.getText().toString());
    } catch (NumberFormatException exception) {
        Toast.makeText(context, yourEditText + " has non Integer value", Toast.LENGTH_SHORT).show();
    }
}

private void multiplyByValueFromEditText(EditText yourEditText) {
    try {
        edTotal *= Integer.valueOf(yourEditText.getText().toString());
    } catch (NumberFormatException exception) {
        Toast.makeText(context, yourEditText + " has non Integer value", Toast.LENGTH_SHORT).show();
    }
}

private void divideByValueFromEditText(EditText yourEditText) {
    try {
        edTotal /= Integer.valueOf(yourEditText.getText().toString());
    } catch (NumberFormatException exception) {
        Toast.makeText(context, yourEditText + " has non Integer value", Toast.LENGTH_SHORT).show();
    }

private void sumAllEditTexts() {
    addValueFromEditText(ed1);
    addValueFromEditText(ed2);
    addValueFromEditText(ed3);
    addValueFromEditText(ed4);
    addValueFromEditText(ed5);
    addValueFromEditText(ed6);
}

}

Edit Added example method on how to use. Or you can simply iterate through your container which contains those EditTextView's

Upvotes: 0

Azamat Ahunjanov
Azamat Ahunjanov

Reputation: 366

I guess you should take each editText and check if this is empty or not ->

int count = 0;
   if(!editTextFirst.getText.toString().equals("")){
     count += Integer.parse(editTextFirst.getText.toString());
   }
   else if(// next editText){
   }

with dividing and multiplying change your count from 0 to 1. Sorry if there is any mistake in java syntax.

Upvotes: 1

Related Questions