Java 数字格式异常错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9754103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:11:10  来源:igfitidea点击:

NumberFormatException Error

javaandroid

提问by Exorcist

the code is:

代码是:

editText2=(EditText) findViewById(R.id.editText2);
editText3=(EditText) findViewById(R.id.editText3);
float from_value= Float.parseFloat(editText2.getText().toString());
editText3.setText(" "+(from_value * 100.0));

And the logcat error is:

而 logcat 错误是:

03-18 03:19:07.847: E/AndroidRuntime(875): Caused by: java.lang.NumberFormatException: Invalid float: ""

03-18 03:19:07.847:E/AndroidRuntime(875):由:java.lang.NumberFormatException:无效浮点数:“”

采纳答案by MByD

Seems like the String in editText2 is empty, so it fails to parse it as float.

似乎 editText2 中的 String 是空的,因此无法将其解析为浮点数。

A possible solution is to check if the String is empty first, and then decide about default value, another is to catch the exception:

一个可能的解决方案是先检查 String 是否为空,然后决定默认值,另一个是捕获异常:

float from_value;
try {
    from_value = Float.parseFloat(editText2.getText().toString());
}
catch(NumberFormatException ex) {
    from_value = 0.0; // default ??
}

回答by Jarle Hansen

Probably a good idea to validate the value you are reading from editText2 before parsing it to float. Make sure it is a valid number first.

在将它解析为浮动之前验证您从 editText2 读取的值可能是一个好主意。首先确保它是一个有效的数字。

回答by xyz

IMHO you are entering some non float value which is causing this error.

恕我直言,您正在输入一些导致此错误的非浮点值。

回答by Anonsage

You should try to avoid using try/catch if at all possible because it is not the preferred method.

如果可能,您应该尽量避免使用 try/catch,因为它不是首选方法。

The correct way to avoid this error is to stop it before it happens.

避免此错误的正确方法是在它发生之前将其停止。

if (mEditText.getText().toString().equals("")) {
    value = 0f;
} else {
    value = Float.parseFloat(mEditText.getText().toString()); 
}

Update: Since you are using EditText, the simplest way to avoid the NumberFormatException is to specify the inputTypeattribute so that it will only accept numbers. XML example: android:inputType="number". Though, you may still get a value larger (or smaller) that the data type can't hold.

更新:由于您使用的是EditText,避免 NumberFormatException 的最简单方法是指定inputType属性,使其只接受数字。XML 示例:android:inputType="number". 但是,您可能仍然会得到数据类型无法容纳的更大(或更小)的值。

Then, depending on your use case, you could even step up a level by using a custom "IntEditText" or custom InputFilterso that you can specify a min and max, and re-use the code across apps.

然后,根据您的用例,您甚至可以通过使用自定义的“IntEditText”或自定义来提高级别,InputFilter以便您可以指定最小值和最大值,并在应用程序中重复使用代码。

回答by doom4s

to me the best way is to do it like this:

对我来说最好的方法是这样做:

editText2=(EditText) findViewById(R.id.editText2);
editText3=(EditText) findViewById(R.id.editText3);

if(!editText2.getText().toString().matches("")){
float from_value= Float.parseFloat(editText2.getText().toString());
editText3.setText(" "+(from_value * 100.0));
}

And also add this line to your xml for editText2

并将这一行添加到您的 xml 以用于 editText2

android:inputType="numberDecimal"

Hope it helps !

希望能帮助到你 !