-1

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!

Luis
  • 123
  • 1
  • 3
  • 18
  • how does your input look like? – Jérôme Apr 25 '17 at 20:32
  • It seems one those input fields might be accepting a string, then when you use parse double it tries parsing the entire string, – Remario Apr 25 '17 at 20:34
  • 1
    My inputs in xml are all like this, with of course different ids.: And, @Remario , I'm running through the instanceof now... will post back soon – Luis Apr 25 '17 at 20:45

1 Answers1

1

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.

Remario
  • 3,813
  • 2
  • 18
  • 25
  • only after then can you perform any arithmetic operations on the input values. – Remario Apr 25 '17 at 20:37
  • also though not a good practice surround all your calculations in a try catch block. And the catch block catch A Exception(super class) for all exceptions. This way we can catch all exceptions if any. – Remario Apr 25 '17 at 20:44
  • what you mean exactly? – Remario Apr 25 '17 at 20:50
  • 1
    Oh I wanted to say return "false", but I'm mistaken either way. Sorry. – Luis Apr 25 '17 at 20:55
  • oh one ay to make sure there is no only number do a regex on the edit text string . – Remario Apr 25 '17 at 20:57
  • Thanks man, sorry for the delay in answering, as I said, my coding skills are pretty limited =D, I'm figuring out where to do the checks. So if I take sometime to get the result, don't worry! I'm still trying. – Luis Apr 25 '17 at 21:09