1

I am designing a basic BMI calculator and I have the calculator working, but I need to convert the calculated answer from a TextView which is a double to an integer to enable me right statements with <> operators based on the calculated answer.
How do I convert the answer from the TextView to an integer?

    v.findViewById(R.id.bmi).setOnClickListener(this);

    return v;
}

public void onClick(View v) {

    switch (v.getId()) {

        case R.id.bmi:

            try {


                EditText w = (EditText) getView().findViewById(R.id.w);
                String value = w.getText().toString();
                EditText h = (EditText) getView().findViewById(R.id.h);
                String value1 = h.getText().toString();

                TextView ans = (TextView) getView().findViewById(R.id.ans);
                Double l1 = Double.parseDouble(value);
                Double l2 = Double.parseDouble(value1);
                ans.setText(Double.toString(l1 / (l2 * l2)));                                     

            }
            catch (Exception e)
            {
                new AlertDialog.Builder(getActivity()).setNeutralButton("OK",null).setMessage("ALL FIELDS ARE REQUIRED TO COMPUTE YOUR BMI! ").show();
                break;
            }

    }

}
}
appersiano
  • 2,670
  • 22
  • 42
  • 3
    You are already doing something similar with doubles: `Double l1 = Double.parseDouble(value);`. If you want integers, do `int l1 = Integer.parseInt(value);` – Phantômaxx Dec 27 '15 at 15:00
  • 1
    I don't get it, your code hasn't got any problem. Why don't you just save your answer when you have calculated it?The question isn't clear. – Abhishek Dec 27 '15 at 15:20
  • the code runs fine, the issue here is I need to write some conditional statements based on the result using some operators , nd this will only work if I convert the computed answer from double to int – Oyubu Obukohwo Dec 27 '15 at 15:25
  • 1
    Alright, so why don't you just cast your answer from Double to Integer? – Abhishek Dec 27 '15 at 15:29
  • when I work with integer app crashes because the user enter number with decimals also – Oyubu Obukohwo Dec 27 '15 at 15:36
  • Tell me if I am getting you correct, you want to calculate answer in double, convert it into integer, do some operations on it? – Abhishek Dec 27 '15 at 15:40
  • exactly what I want to do – Oyubu Obukohwo Dec 27 '15 at 15:48

1 Answers1

1

First, The conversion is NOT from textview to int. textView aint a datatype. If you want the result in int use parseInt() method. int i = Integer.parseInt(value).

[EDIT]

Try this, this will work.

TextView tv = (TextView) findViewById(R.id.tv);
EditText et = (EditText) findViewById(R.id.et);

try {
double d = Double.valueOf(String.valueOf(et.getText()));
tv.setText(String.valueOf( (int) d));
}catch (NumberFormatException e){
Toast.makeText(MainActivity.this, "Enter valid number.", Toast.LENGTH_SHORT).show();
}

I have simplified it for your understanding. The code can still be optimized.

Srujan Barai
  • 2,295
  • 4
  • 29
  • 52