Java:输入十进制数时崩溃 [RESOLVE]

Java : Crash when put decimal number [RESOLVE]

好的,我的问题是:我创建了一个应用程序来计算我的 IMC(法语单词)。但是当我尝试在 EditText 中以十进制形式输入我的身高或体重时,它 return 我这个错误 :

03-27 18:52:25.459 31274-31274/fr.imc.oxyz0n359.imc E/AndroidRuntime: FATAL EXCEPTION: main
                                                                  Process: fr.imc.oxyz0n359.imc, PID: 31274
                                                                  java.lang.NumberFormatException: For input string: "1.5"
                                                                      at java.lang.Integer.parseInt(Integer.java:521)
                                                                      at java.lang.Integer.parseInt(Integer.java:556)
                                                                      at fr.imc.oxyz0n359.imc.MainActivity.onClick(MainActivity.java:27)
                                                                      at android.view.View.performClick(View.java:5675)
                                                                      at android.view.View$PerformClick.run(View.java:22641)
                                                                      at android.os.Handler.handleCallback(Handler.java:836)
                                                                      at android.os.Handler.dispatchMessage(Handler.java:103)
                                                                      at android.os.Looper.loop(Looper.java:203)
                                                                      at android.app.ActivityThread.main(ActivityThread.java:6251)
                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)

这是我的代码:

public class MainActivity extends AppCompatActivity {

private TextView Resultat;
private Button Calcul;
private EditText Poids, Taille;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Resultat = (TextView)findViewById(R.id.Resultat);
    Calcul = (Button)findViewById(R.id.Calcul);
    Poids = (EditText)findViewById(R.id.Poids);
    Taille = (EditText)findViewById(R.id.Taille);
    Calcul.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int Etape = Integer.parseInt(Taille.getText().toString())*Integer.parseInt(Taille.getText().toString());
            int Result = Integer.parseInt(Poids.getText().toString())/Etape;
            Resultat.setText("IMC : " + Result);
        }
    });
}
}

感谢您的回答,OxYz0n359。

编辑:我解决了问题,感谢您的回答我将代码更改为:

public class MainActivity extends AppCompatActivity {

private TextView Resultat;
private Button Calcul;
private EditText Poids, Taille;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Resultat = (TextView)findViewById(R.id.Resultat);
    Calcul = (Button)findViewById(R.id.Calcul);
    Poids = (EditText)findViewById(R.id.Poids);
    Taille = (EditText)findViewById(R.id.Taille);
    Calcul.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            double etape = Double.parseDouble(Taille.getText().toString())*Double.parseDouble(Taille.getText().toString());
            double result = Double.parseDouble(Poids.getText().toString())/etape;
            Resultat.setText("IMC : " + result);
        }
    });
}
}

"1.5" 不是整数,您需要将其解析为 Double,然后才以最适合您的方式转换为整数。另外,java 变量名以小写字母开头,我会建议你跟上这个习惯。

您没有解析整数。调用 parseInt('code') 时,它不理解小数点是什么。尝试使用 parseDouble('code') 或其等价物。你也会产生一个错误的答案,因为你试图在 int 数据类型中存储一个 double。

"1.5" 无法解析为 integer。它是数字的浮动表示。
而例外情况:

java.lang.NumberFormatException: For input string: "1.5"

您需要的是Double.parseDouble()Float.parseFloat()
如果你不关心结果中的舍入部分,你仍然可以舍入它:

double taille = Double.parseDouble(Taille.getText().toString());
int Etape = (int) (taille * taille);

如果您关心圆形部分,您应该将 int 变量替换为浮动数字类型,例如 double(或浮点数),并可能使用 BigDecimal 来执行计算和获得更精确的结果。

java doc 中所述,触发 NumberFormatException 是因为您解析的字符串不代表整数。实际上,“1.5”是双精度而不是整数。

您应该考虑使用 Double#parseDouble,因为在您的用例中高度是两倍