0

I have an app which shows a string in an EditText, this string is the result of the operation of two other doubles the user types in two different EditTexts. The problem is that I want the result of the operation to be shown in the third EditText, but for that it has to be a string. Therefore I change the result by the toString method.

The problem starts here, I want the double that will be a string to have only one decimal. For that I used DecimalFormat and created the df format "#.#". And then I changed the text that would be shown in the last EditText to the new double variable with only one decimal (obviously changing it to String).

DecimalFormat df = new DecimalFormat("#.#");
double BMI_trimmed = Double.parseDouble(df.format(BMI));

final EditText BMIResult = (EditText)findViewById(R.id.BMIResult);
BMIResult.setText(Double.toString(BMI_trimmed));

Here I leave you all the code of the myButtonListenerMethod:

public void myButtonListenerMethod(){
    button = (Button)findViewById(R.id.button);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final EditText heighText = (EditText)findViewById(R.id.heightInput);
            String heighStr = heighText.getText().toString();
            double height = Double.parseDouble(heighStr);

            final EditText weighText = (EditText)findViewById(R.id.weightInput);
            String weighStr = weighText.getText().toString();
            double weight = Double.parseDouble(weighStr);

            double BMI = (weight)/(height*height);

            DecimalFormat df = new DecimalFormat("#.#");
            double BMI_trimmed = Double.parseDouble(df.format(BMI));

            final EditText BMIResult = (EditText)findViewById(R.id.BMIResult);
            BMIResult.setText(Double.toString(BMI_trimmed));
        }
    });
}

This app runs perfectly on the AVD, I've runned it in three already. But when I run it in a real device and click the button that starts the myButtonListenerMethod, it stops working suddenly and shuts down. The Terminal gives the following error message:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.bmicalculator, PID: 19058
    java.lang.NumberFormatException: For input string: "24,2"
        at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1306)

If anyone knows what the problem is, please tell me I'll try. Honestly I don't understand why it runs in the AVD but it doesn't properly in a real device. Any idea?

Kasım Özdemir
  • 5,414
  • 3
  • 18
  • 35
  • Well, depending on the device's locale, the input "24,2" might not be a valid number. Can you verify the locale for us? – markspace Apr 12 '20 at 14:14
  • Some of the locales use the comma as decimal separator and your device might be using one such locale. Pl check this SO [question](https://stackoverflow.com/questions/888088/how-do-i-convert-a-string-to-double-in-java-using-a-specific-locale). – Rajen Raiyarela Apr 12 '20 at 14:22

3 Answers3

1

You already get the value rounded as you want and as a string from the formatter. Don't try to parse it, just display it.

BMIResult.setText(df.format(BMI));
Joni
  • 108,737
  • 14
  • 143
  • 193
0

The problem is probably your phone Locale. Some phones use . as a separator and some use , as separator. Try replacing all "," with "." before parsing from String to Double.

Also try using this answer: https://stackoverflow.com/a/7559011/2249224

Rafael Skubisz
  • 460
  • 3
  • 9
-1
double BMI_trimmed = Double.parseDouble(df.format(BMI));
String yourResult = String.valueOf(BMI_trimmed);  

Happy coding

mohit
  • 88
  • 9