简体   繁体   中英

Making parsing easier in android EditText

I have an app with a couple of EditTexts. The user leaves on empty and the program calculates the empty variable. This is the onClickListener for the button that initiates it all. I was looking to get all the parsing done before the switch so that I only need to parse ALL entries once and then have a 0 put in for the empty one as this would give an error.

How can I streamline this code?

public void buttonCalc() {
    calc.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            pv = (EditText) getActivity().findViewById(R.id.pv_pv);
            fv = (EditText) getActivity().findViewById(R.id.pv_fv);
            r = (EditText) getActivity().findViewById(R.id.pv_r);
            n = (EditText) getActivity().findViewById(R.id.pv_n);
            t = (EditText) getActivity().findViewById(R.id.pv_t);
            answer = (TextView) getActivity().findViewById(R.id.pv_answer);
            //check for fields and choice
            if (pv.getText().toString().equals("")) {emptyfields++;choice = 1;}
            if (fv.getText().toString().equals("")) {emptyfields++;choice = 2;}
            if (r.getText().toString().equals("")) {emptyfields++;choice = 3;}
            if (n.getText().toString().equals("")) {emptyfields++;choice = 4;}
            if (t.getText().toString().equals("")) {emptyfields++;choice = 5;}
            if (emptyfields > 1) {MiscMethods.ErrorToast(1);}
            else{
                emptyfields--;
                try {

                } catch (Exception e) {
                }
                switch (choice) {
                case 1:// pv
                    double fv1 = Double.parseDouble(fv.getText().toString());
                    double r1 = Double.parseDouble(r.getText().toString());
                    double n1 = Double.parseDouble(n.getText().toString());
                    double t1 = Double.parseDouble(t.getText().toString());
                    double result1 = fv1/(Math.pow(1+(r1/n1),n1*t1));
                    result1 = MiscMethods.rounder(result1, 2);
                    answer.setText(answer_pv + result1);
                    break;
                case 2:// fv
                    double pv2 = Double.parseDouble(pv.getText().toString());
                    double r2 = Double.parseDouble(r.getText().toString());
                    double n2 = Double.parseDouble(n.getText().toString());
                    double t2 = Double.parseDouble(t.getText().toString());
                    double result2 = pv2*(Math.pow(1 + (r2 / n2), n2 * t2));
                    result2 = MiscMethods.rounder(result2, 2);
                    answer.setText(answer_fv + result2);
                    break;
                case 3:// r
                    double pv3 = Double.parseDouble(pv.getText().toString());
                    double fv3 = Double.parseDouble(fv.getText().toString());
                    double n3 = Double.parseDouble(n.getText().toString());
                    double t3 = Double.parseDouble(t.getText().toString());
                    double result3 = ((Math.pow(fv3 / pv3, (1 / (n3 * t3)))) - 1)* n3;
                    result3 = MiscMethods.rounder(result3, 4);
                    answer.setText(answer_r +" "+ result3 + " / " + (result3*100)+"%");
                    break;
                case 4:// n
                    MiscMethods.ErrorToast(2);
                    break;
                case 5:// t
                    double pv5 = Double.parseDouble(pv.getText().toString());
                    double fv5 = Double.parseDouble(fv.getText().toString());
                    double n5 = Double.parseDouble(n.getText().toString());
                    double r5 = Double.parseDouble(r.getText().toString());
                    double result5 = (Math.log((fv5 / pv5))) / ((Math.log((1 + (r5 / n5)))) * n5);
                    result5 = MiscMethods.rounder(result5, 2);
                    answer.setText(answer_t +" "+ result5);
                    break;
                default:
                    MiscMethods.ErrorToast(3);
                    break;
                }// switch ends
            }// else ends

        }

    });
}

What you could do is something like this:

//initialise doubles with default 0 value when string is empty
double pvDbl = getDouble(pv);
double fvDbl = getDouble(fv);
//etc

and the getDouble method could be something like:

private static double getDouble(EditText et) {
    if(et.getText().length() == 0) {
        return 0;
    }
    try {
        return Double.parseDouble(et.getText().toString());
    } catch (NumberFormatException e) {
        return 0; //default value for invalid entries?
                  //Or maybe show an error message?
    }
}

You can then replace your switch with if statements:

if (pvDbl == 0 ) {
    //formula for pv null
} else if (fvDbl == 0) {
    //formula for fv null
} //etc. 

Note that you also need to deal with 0 entries (in the code I propose, you can't make the differnece between an empty entry "" and an entry of "0" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM