Androyds
Androyds

Reputation: 67

Check if the for loop is done before proceed to a statement

Got a few problem here. I have created a arrays of EditText and it is working fine. Now I am getting an error when one of the EditText is empty. Here is my code:

int[] textIDs = new int[] {R.id.etFirstName, R.id.etLastName, R.id.etEmail, R.id.etAddress, R.id.etCity, R.id.etRegion, R.id.etMobile, R.id.etLandLine, R.id.etYear, R.id.etMonth, R.id.etDay };
        for(int j=0; j<textIDs.length; j++) {
            EditText editText = (EditText) findViewById(textIDs[j]);
            if(editText.getText().toString().trim().equals(""))
            {
                 // editText is empty
                Toast.makeText(MainActivity.this, "Request cannot performed..\n Please ensure all fields are filled", Toast.LENGTH_SHORT).show();
                break;
            } 
            else 
            {
                 // editText is not empty
                Toast.makeText(MainActivity.this, "12", Toast.LENGTH_SHORT).show();
            }

The main problem using this code is that the loop continues to do its function that this code Toast.makeText(MainActivity.this, "12", Toast.LENGTH_SHORT).show(); continues to show in every loop. Is there any way that this Toast shows after the looping is done?

Upvotes: 1

Views: 1162

Answers (3)

hashtpaa
hashtpaa

Reputation: 389

int[] textIDs = new int[] {R.id.etFirstName, R.id.etLastName, R.id.etEmail, R.id.etAddress, R.id.etCity, R.id.etRegion, R.id.etMobile, R.id.etLandLine, R.id.etYear, R.id.etMonth, R.id.etDay };
Toast t; 

    for(int j=0; j<textIDs.length; j++) {
        EditText editText = (EditText) findViewById(textIDs[j]);
        if(editText.getText().toString().trim().equals(""))
        {// editText is empty
            t = Toast.makeText(MainActivity.this, "Request cannot performed..\n Please ensure all fields are filled", Toast.LENGTH_SHORT);
            break;
        } 
        else 
        {
             // editText is not empty
            static Toast toast = Toast.makeText(MainActivity.this, "12", Toast.LENGTH_SHORT);
            t = toast;
        }
    }
    t.show();

Upvotes: 0

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33544

Place it out of the for loop..

boolean isNotEmpty = false;

    for(int j=0; j<textIDs.length; j++) {
                EditText editText = (EditText) findViewById(textIDs[j]);
                if(editText.getText().toString().trim().equals(""))
                {
                     // editText is empty
                    Toast.makeText(MainActivity.this, "Request cannot performed..\n Please ensure all fields are filled", Toast.LENGTH_SHORT).show();

                    isNotEmpty = false;   // Marking as Empty
                    break;
                } 
                else 
                {
                     // editText is not empty

                    isNotEmpty = true     // Marking as Non-Empty

                }
             }
    if (isNotEmpty){
    Toast.makeText(MainActivity.this, "12", Toast.LENGTH_SHORT).show();
    }

Upvotes: 1

WIllJBD
WIllJBD

Reputation: 6154

    boolean doShowToast = false;
    int[] textIDs = new int[] {R.id.etFirstName, R.id.etLastName, R.id.etEmail, R.id.etAddress, R.id.etCity, R.id.etRegion, R.id.etMobile, R.id.etLandLine, R.id.etYear, R.id.etMonth, R.id.etDay };
    for(int j=0; j<textIDs.length; j++) {
        EditText editText = (EditText) findViewById(textIDs[j]);
        if(editText.getText().toString().trim().equals(""))
        {// editText is empty
            Toast.makeText(MainActivity.this, "Request cannot performed..\n Please ensure all fields are filled", Toast.LENGTH_SHORT).show();
            break;
        } 
        else 
        {
             // editText is not empty
            doShowToast = true;
        }
    }
    if(doShowToast){
         Toast.makeText(MainActivity.this, "12", Toast.LENGTH_SHORT).show();
    }

by using a boolean you will only show the toast for the else statement once, after the loop has exited, and only if the else statement was called, so only if "editText is not empty"

Upvotes: 0

Related Questions