Search code examples
javaandroidinfinity

Java calculation returning infinity


it's been a while since I last asked anything around here! I'm making an Android app, and so, having this problem when running this code.:

Button calc = (Button) findViewById(R.id.btnCalculate);
    calc.setOnClickListener(new View.OnClickListener()

    {
        public void onClick(View v) {

            EditText number1 = (EditText) findViewById(R.id.dnp);
            EditText number2 = (EditText) findViewById(R.id.ponte);
            EditText number3 = (EditText) findViewById(R.id.aro);
            EditText number4 = (EditText) findViewById(R.id.ed);
            EditText number5 = (EditText) findViewById(R.id.esf);
            EditText number6 = (EditText) findViewById(R.id.curvint);
            EditText number7 = (EditText) findViewById(R.id.curvb);
            EditText number8 = (EditText) findViewById(R.id.ind);
            EditText number9 = (EditText) findViewById(R.id.espmin);
            TextView display = (TextView) findViewById(R.id.display);

            double dnp = Double.parseDouble(number1.getText().toString());
            double ponte = Double.parseDouble(number2.getText().toString());
            double aro = Double.parseDouble(number3.getText().toString());
            double ed = Double.parseDouble(number4.getText().toString());
            double esf = Double.parseDouble(number5.getText().toString());
            double curvint = Double.parseDouble(number6.getText().toString());
            double curvb = Double.parseDouble(number7.getText().toString());
            double ind = Double.parseDouble(number8.getText().toString());
            double espmin = Double.parseDouble(number9.getText().toString());

            double R1;
            double R2;
            double DT; //Decentração total
            double DM; //Decentração monocular
            double diametro;
            double S1;
            double S2;
            double espessurafinal;

            if (esf < 0)
            {

                R1 = (ind - 1 / curvb) * 1000;
                R2 = (ind - 1 / curvint) * 1000;
                DT = (aro+ponte)-dnp;
                DM = (DT / 2);
                diametro = (ed + 2 * DM) / 2;
                S1 = R1 - (Math.sqrt(Math.exp(R1)-Math.exp(diametro)));
                S2 = R2 - (Math.sqrt(Math.exp(R2)-Math.exp(diametro)));
                espessurafinal = (S1 - S2) + espmin;
                display.setText(espessurafinal + "" + "\nMilímetros");
            }

            else {

                R1 = (ind - 1 / curvb) * 1000;
                R2 = (ind - 1 / curvint) * 1000;
                DT = (aro+ponte)-dnp;
                DM = (DT / 2);
                diametro = (ed + 2 * DM) / 2;
                S1 = R1 - (Math.sqrt(Math.exp(R1)-Math.exp(diametro)));
                S2 = R2 - (Math.sqrt(Math.exp(R2)-Math.exp(diametro)));
                espessurafinal = espmin - (S1 + S2);
                display.setText(espessurafinal + "\nMilímetros");
            }
        }
    });

I am probably missing something very trivial here, and my coding skills are very limited, but this code snippet returns "Infinity", when I press the calculate button. Now, I was receiving NaN before and managed to get rid of it, but only to see it become infinity! I researched and found out that it's due to some number being divided by 0, but I don't see that... (Oh and yes, I'm making sure all the numbers being input are not 0)

May be just my coding that put things in their wrong places? (And yes, I know I could make the code a lot better by using functions and all that... but I'm learning people, be gentle to me lol ).

Or, my math is very wrong and returning something extremelly high?

Thanks in advance everyone!


Solution

  • It seems that some of input is not actually a number, or a number mixed with some Random string. Do a validation check on the edit text values by checking if the input is a instanceof Double. input instanceof Double

      if( input instanceof Double) {
        System.out.println("input is a Double");
      }
    

    Before you can check instance of, make sure the input is clean, with only numerals.Run a java regular expression ^[0-9]*$ on the edit text , if true then only numbers were inputted, then use Double.valueOf(val) or Double.parse() the only difference is the latter returns a object of type Double where as the other only returns a primitive type.