Göksel ÖZER
Göksel ÖZER

Reputation: 267

How to increase edittexts' number in for loop

I have 8 EditText and I want to check whether these EditTexts are empty or not.
This is my code:

if (edt1.getText().toString().equals("K")
      &&!edt2.getText().toString().equals("")
      &&!edt3.getText().toString().equals("")
      &&!edt4.getText().toString().equals("")
      &&!edt5.getText().toString().equals("")
      &&!edt6.getText().toString().equals("")
      &&!edt7.getText().toString().equals("")
      &&!edt8.getText().toString().equals("")){
    next();
}

This is what i want:

int count=0;
for(int i=1;i<=8,i++){
    if(!edt.getText().toString().equals(""))//how can I increase the number of edt, if i=1, edt1 and if i=2, edt2... 
        count++;
}
if(count==8){
    next();
}

Upvotes: 1

Views: 122

Answers (1)

Dharmendra Vishwakarma
Dharmendra Vishwakarma

Reputation: 516

Read the EditText by line by line in an array. That would be much simpler. Also, you can utilise the String Utils http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html to check empty string or you can go with the default null and empty string check.

Put the references like edt1,ed2,edt3 into an array and loop over it. That would work.

int count=0;
EditText edt[] = {edt1,edt2,edt3,edt4,edt5,edt6,edt7,edt8}
for(int i=0;i<=7,i++){
    if(edt[0].getText().toString().equals("K") || !edt[i].getText().toString().equals(""))
        count++;
}
if(count==8){
    next();
}

Upvotes: 2

Related Questions