Handler for an Android form to calculate a cross product Button crossButton = (Button) findViewById(R.id.crossButton);
crossButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if (!x1.getText().toString().equals("")) {
            if (!x2.getText().toString().equals("")) {
                if (!x3.getText().toString().equals("")) {
                    if (!y1.getText().toString().equals("")) {
                        if (!y2.getText().toString().equals("")) {
                            if (!y3.getText().toString().equals("")) {
                                double x1no = Double.parseDouble(x1.getText().toString());
                                double x2no = Double.parseDouble(x2.getText().toString());
                                double x3no = Double.parseDouble(x3.getText().toString());
                                double y1no = Double.parseDouble(y1.getText().toString());
                                double y2no = Double.parseDouble(y2.getText().toString());
                                double y3no = Double.parseDouble(y3.getText().toString());
                                double z1 = (x2no * y3no) - (x3no * y2no);
                                double z2 = (x3no * y1no) - (x1no * y3no);
                                double z3 = (x1no * y2no) - (x2no * y1no);
                                resultText.setText(getString(R.string.decimal_vector_output, z1, z2, z3));
                            } else { y3.setError("Needs value"); y3.requestFocus(); }
                        } else { y2.setError("Needs value"); y2.requestFocus(); }
                    } else { y1.setError("Needs value"); y1.requestFocus(); }
                } else { x3.setError("Needs value"); x3.requestFocus(); }
            } else { x2.setError("Needs value"); x2.requestFocus(); }
        } else { x1.setError("Needs value"); x1.requestFocus(); }
    }
});


How would I go about cleaning up these if statements and whatnot?

I've tried a few OR conditions and it seemed to break.

Help would be appeciated.
I expect x/y 1-3 are textfields, though not enough context to be entirely certain, going from that assumption:


Create a helper method to check for value:

private boolean hasValue(TextField field) {
    return !field.getText().toString().isEmpty();
}

Store all textfields in an array.
Loop through and validate each one.

for (TextField field : textFields) {
   if (!hasValue(field)) {
      field.setError("needs value");
      field.requestFocus();
   }
}



Depending on your needs, I would apply this to use break, and perhaps apply it as a boolean returning method that ensures a validated state before continuing application logic.


Once the validation is done and completed (why a boolean returning method for step 3 could be helpful), proceed with the application logic.