Q:

How to write a complex average equation in android?

I have an application which calculates two types of average, one is simple, and the second is more complex and I can't figure out how to write the equation of it. The way I have written it is how it should be calculated, but I get error on syntax like "{". The equation should do this: the sum of the values divided to the number of how many are entered, then the result multiplied with 3 plus another value, and then, the result divided to 4. Hope you understood exactly. Thanks ! !
Here is my:
Equation:
double total2 = {[(i + j + k + l + m + n + o + p + q + r)/numarValori2] x 3 + r}/(double)4;

 That happens when I try to calulate the first average, not the second !!!
Medii.java:
package com.cngcnasaud.orar;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

public class Medii extends Activity {

    EditText txtNum1;
    EditText txtNum2;
    EditText txtNum3;
    EditText txtNum4;
    EditText txtNum5;
    EditText txtNum6;
    EditText txtNum7;
    EditText txtNum8;
    EditText txtTotal;
    EditText txtNum10;
    EditText txtNum11;
    EditText txtNum12;
    EditText txtNum13;
    EditText txtNum14;
    EditText txtNum15;
    EditText txtNum16;
    EditText txtNum17;
    EditText txtNum18;
    EditText txtNum19;
    EditText txtNum20;
    EditText txtTotal2;
    Button btnCal;
    Button btnCal2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.medii);

        txtNum1 = (EditText) findViewById(R.id.editText1);
        txtNum2 = (EditText) findViewById(R.id.editText2);
        txtNum3 = (EditText) findViewById(R.id.editText3);
        txtNum4 = (EditText) findViewById(R.id.editText4);
        txtNum5 = (EditText) findViewById(R.id.editText5);
        txtNum6 = (EditText) findViewById(R.id.editText6);
        txtNum7 = (EditText) findViewById(R.id.editText7);
        txtNum8 = (EditText) findViewById(R.id.editText8);
        txtTotal = (EditText) findViewById(R.id.editText9);
        btnCal = (Button) findViewById(R.id.bmedii);
        txtNum10 = (EditText) findViewById(R.id.editText10);
        txtNum11 = (EditText) findViewById(R.id.editText11);
        txtNum12 = (EditText) findViewById(R.id.editText12);
        txtNum13 = (EditText) findViewById(R.id.editText13);
        txtNum14 = (EditText) findViewById(R.id.editText14);
        txtNum15 = (EditText) findViewById(R.id.editText15);
        txtNum16 = (EditText) findViewById(R.id.editText16);
        txtNum17 = (EditText) findViewById(R.id.editText17);
        txtNum18 = (EditText) findViewById(R.id.editText18);
        txtNum19 = (EditText) findViewById(R.id.editText19);
        txtTotal2 = (EditText) findViewById(R.id.editText20);
        btnCal2 = (Button) findViewById(R.id.bmedii12);

        btnCal.setOnClickListener(new ClickButton ());
        btnCal2.setOnClickListener(new ClickButton());

    }

    private class ClickButton implements Button.OnClickListener{

        @Override
        public void onClick(View v) {

            int a = txtNum1.getText().toString().isEmpty()?0:Integer.parseInt(txtNum1.getText().toString());
            int b = txtNum2.getText().toString().isEmpty()?0:Integer.parseInt(txtNum2.getText().toString());
            int c = txtNum3.getText().toString().isEmpty()?0:Integer.parseInt(txtNum3.getText().toString());
            int d = txtNum4.getText().toString().isEmpty()?0:Integer.parseInt(txtNum4.getText().toString());
            int e = txtNum5.getText().toString().isEmpty()?0:Integer.parseInt(txtNum5.getText().toString());
            int f = txtNum6.getText().toString().isEmpty()?0:Integer.parseInt(txtNum6.getText().toString());
            int g = txtNum7.getText().toString().isEmpty()?0:Integer.parseInt(txtNum7.getText().toString());
            int h = txtNum8.getText().toString().isEmpty()?0:Integer.parseInt(txtNum8.getText().toString());
            int numarValori = (a == 0 ? 0 : 1) + (b == 0 ? 0 : 1) + (c == 0 ? 0 : 1) + (d == 0 ? 0 : 1) + (e == 0 ? 0 : 1) + (f == 0 ? 0 : 1) + (g == 0 ? 0 : 1) + (h == 0 ? 0 : 1);
            double total = (a + b + c + d + e + f + g + h)/(double)numarValori;

            txtTotal.setText(Double.toString(total));

            int i = txtNum10.getText().toString().isEmpty()?0:Integer.parseInt(txtNum10.getText().toString());
            int j = txtNum11.getText().toString().isEmpty()?0:Integer.parseInt(txtNum11.getText().toString());
            int k = txtNum12.getText().toString().isEmpty()?0:Integer.parseInt(txtNum12.getText().toString());
            int l = txtNum13.getText().toString().isEmpty()?0:Integer.parseInt(txtNum13.getText().toString());
            int m = txtNum14.getText().toString().isEmpty()?0:Integer.parseInt(txtNum14.getText().toString());
            int n = txtNum15.getText().toString().isEmpty()?0:Integer.parseInt(txtNum15.getText().toString());
            int o = txtNum16.getText().toString().isEmpty()?0:Integer.parseInt(txtNum16.getText().toString());
            int p = txtNum17.getText().toString().isEmpty()?0:Integer.parseInt(txtNum17.getText().toString());
            int q = txtNum18.getText().toString().isEmpty()?0:Integer.parseInt(txtNum18.getText().toString());
            int r = txtNum19.getText().toString().isEmpty()?0:Integer.parseInt(txtNum19.getText().toString());
            int numarValori2 = (i == 0 ? 0 : 1) + (j == 0 ? 0 : 1) + (k == 0 ? 0 : 1) + (l == 0 ? 0 : 1) + (m == 0 ? 0 : 1) + (n == 0 ? 0 : 1) + (o == 0 ? 0 : 1) + (p == 0 ? 0 : 1) + (q == 0 ? 0 : 1) + (r == 0 ? 0 : 1);

            double total2 = {[(i + j + k + l + m + n + o + p + q + r)/numarValori2] x 3 + r}/(double)4;

        }

    }

}

L.E:
Logcat:
03-23 12:52:26.488: E/AndroidRuntime(938): FATAL EXCEPTION: main
03-23 12:52:26.488: E/AndroidRuntime(938): java.lang.ArithmeticException: divide by zero
03-23 12:52:26.488: E/AndroidRuntime(938):  at com.cngcnasaud.orar.Medii$ClickButton.onClick(Medii.java:99)
03-23 12:52:26.488: E/AndroidRuntime(938):  at android.view.View.performClick(View.java:4204)
03-23 12:52:26.488: E/AndroidRuntime(938):  at android.view.View$PerformClick.run(View.java:17355)
03-23 12:52:26.488: E/AndroidRuntime(938):  at android.os.Handler.handleCallback(Handler.java:725)
03-23 12:52:26.488: E/AndroidRuntime(938):  at android.os.Handler.dispatchMessage(Handler.java:92)
03-23 12:52:26.488: E/AndroidRuntime(938):  at android.os.Looper.loop(Looper.java:137)
03-23 12:52:26.488: E/AndroidRuntime(938):  at android.app.ActivityThread.main(ActivityThread.java:5041)
03-23 12:52:26.488: E/AndroidRuntime(938):  at java.lang.reflect.Method.invokeNative(Native Method)
03-23 12:52:26.488: E/AndroidRuntime(938):  at java.lang.reflect.Method.invoke(Method.java:511)
03-23 12:52:26.488: E/AndroidRuntime(938):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-23 12:52:26.488: E/AndroidRuntime(938):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-23 12:52:26.488: E/AndroidRuntime(938):  at dalvik.system.NativeStart.main(Native Method)

Line 99:
double total2 = (((i + j + k + l + m + n + o + p + q + r)/numarValori2) * 3 + r)/(double)4;

** That happens why I calculate the first average, not the second !

A:

Just replace the [] and {} with () in the equation, you can only use () because [] and {} have special meaning in Java.
EDIT 1:
The logcat output tells you pretty much exactly what the problem is: 
java.lang.ArithmeticException: divide by zero

You are diving by zero and since the only variable you are diving by is numarValori and numarValori2 one of these has to be 0. 
What I think is happening is this: 
When You click the calculate button you are performing both calculations. When nothing has been entered for the second calculation every input variable defaults back to 0 and when all variables are 0 so is numarValori2. When the calculation is then performed it will divide by zero.
I can suggest a few changes to your code to improve it, give me a few minutes and I will edit my answer.
EDIT 2:
Here is my improved source code. I didn't test it (I will if I find the time in about half an hour) but it should work:
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Medii extends Activity {

    private EditText txtNum1;
    private EditText txtNum2;
    private EditText txtNum3;
    private EditText txtNum4;
    private EditText txtNum5;
    private EditText txtNum6;
    private EditText txtNum7;
    private EditText txtNum8;
    private EditText txtTotal;
    private EditText txtNum10;
    private EditText txtNum11;
    private EditText txtNum12;
    private EditText txtNum13;
    private EditText txtNum14;
    private EditText txtNum15;
    private EditText txtNum16;
    private EditText txtNum17;
    private EditText txtNum18;
    private EditText txtNum19;
    private EditText txtNum20;
    private EditText txtTotal2;
    private Button btnCal;
    private Button btnCal2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.medii);

        txtNum1 = (EditText) findViewById(R.id.editText1);
        txtNum2 = (EditText) findViewById(R.id.editText2);
        txtNum3 = (EditText) findViewById(R.id.editText3);
        txtNum4 = (EditText) findViewById(R.id.editText4);
        txtNum5 = (EditText) findViewById(R.id.editText5);
        txtNum6 = (EditText) findViewById(R.id.editText6);
        txtNum7 = (EditText) findViewById(R.id.editText7);
        txtNum8 = (EditText) findViewById(R.id.editText8);
        txtTotal = (EditText) findViewById(R.id.editText9);
        btnCal = (Button) findViewById(R.id.bmedii);
        txtNum10 = (EditText) findViewById(R.id.editText10);
        txtNum11 = (EditText) findViewById(R.id.editText11);
        txtNum12 = (EditText) findViewById(R.id.editText12);
        txtNum13 = (EditText) findViewById(R.id.editText13);
        txtNum14 = (EditText) findViewById(R.id.editText14);
        txtNum15 = (EditText) findViewById(R.id.editText15);
        txtNum16 = (EditText) findViewById(R.id.editText16);
        txtNum17 = (EditText) findViewById(R.id.editText17);
        txtNum18 = (EditText) findViewById(R.id.editText18);
        txtNum19 = (EditText) findViewById(R.id.editText19);
        txtTotal2 = (EditText) findViewById(R.id.editText20);
        btnCal2 = (Button) findViewById(R.id.bmedii12);

        btnCal.setOnClickListener(new SimpleAverageClickListener());
        btnCal2.setOnClickListener(new ComplexAverageClickListener());

    }

    private double getNumberFromEditText(EditText editText, double defaultValue) throws NumberFormatException {
        if (editText != null) {
            Editable editable = editText.getText();
            if (editable != null) {
                return Double.parseDouble(editable.toString());
            }
        }
        return defaultValue;
    }

    private boolean isEditTextEmpty(EditText editText) {
        if (editText != null) {
            Editable editable = editText.getText();
            if (editable != null) {
                return editable.toString().isEmpty();
            }

        }
        return true;
    }

    private double getAverageOfEditTexts(EditText... editTexts) throws NumberFormatException{
        double divider = 0.0;
        double sum = 0.0;
        for (EditText editText : editTexts) {
            if (!isEditTextEmpty(editText)) {
                divider++;
                sum += getNumberFromEditText(editText, 0.0);
            }
        }
        return sum / divider;
    }

    private class SimpleAverageClickListener implements Button.OnClickListener {

        @Override
        public void onClick(View v) {

            try {
                double average = getAverageOfEditTexts(txtNum1, txtNum2, txtNum3, txtNum4, txtNum5, txtNum6, txtNum7, txtNum8);
                txtTotal.setText(String.valueOf(average));
            } catch (NumberFormatException e) {
                // Exception occurred because of illegal input, do nothing.
            }
        }
    }

    private class ComplexAverageClickListener implements Button.OnClickListener {

        @Override
        public void onClick(View v) {

            try {
                double average = getAverageOfEditTexts(txtNum10, txtNum11, txtNum12, txtNum13, txtNum14, txtNum15, txtNum16, txtNum17, txtNum18);
                double total = (average * 3 + getNumberFromEditText(txtNum19, 0.0)) / 4.0;
                txtTotal2.setText(String.valueOf(total));
            } catch (NumberFormatException e) {
                // Exception occurred because of illegal input, do nothing.
            }
        }
    }
}

