Android - NumberFormatException 问题
Android - NumberFormatException Issue
输入带小数点的值时,以下代码出现问题。不管我输入 56. 还是 56.3 或 56.345,它仍然会使应用程序崩溃并抛出 NumberFormatException。
这是抛出异常的代码:
private boolean isNumeric(String str) {
try{
Float.parseFloat(str);
}
catch(NumberFormatException e) {
return false;
}
return true;
}
这是 logcat 的例外情况:
05-14 08:54:08.127 31142-31142/com.ritchieengineering.yellowjacket E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.ritchieengineering.yellowjacket, PID: 31142
java.lang.NumberFormatException: Invalid int: "2.5"
at java.lang.Integer.invalidInt(Integer.java:137)
at java.lang.Integer.parse(Integer.java:374)
at java.lang.Integer.parseInt(Integer.java:365)
at java.lang.Integer.parseInt(Integer.java:331)
at java.lang.Integer.valueOf(Integer.java:489)
at com.ritchieengineering.yellowjacket.fragment.TargetSubcoolingFragment.isValidValue(TargetSubcoolingFragment.java:164)
at com.ritchieengineering.yellowjacket.fragment.TargetSubcoolingFragment.onOptionsItemSelected(TargetSubcoolingFragment.java:89)
at android.app.Fragment.performOptionsItemSelected(Fragment.java:1806)
at android.app.FragmentManagerImpl.dispatchOptionsItemSelected(FragmentManager.java:1978)
at android.app.Activity.onMenuItemSelected(Activity.java:2711)
at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1094)
at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:645)
at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:210)
at android.view.View.performClick(View.java:4637)
at android.view.View$PerformClick.run(View.java:19422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5586)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
输入56.3或56.345时如何避免抛出这个异常?我如何检查以确保人们只输入带一位小数的值,例如 56.3?任何帮助将不胜感激。
听起来您的本地文化设置为使用 ,
作为小数点分隔符而不是 .
。您可以使用 NumberFormat
明确设置您的本地文化:
NumberFormat format = NumberFormat.getInstance(Locale.FRENCH);
Number number = format.parse("1,234");
float f = number.floatValue();
输入带小数点的值时,以下代码出现问题。不管我输入 56. 还是 56.3 或 56.345,它仍然会使应用程序崩溃并抛出 NumberFormatException。
这是抛出异常的代码:
private boolean isNumeric(String str) {
try{
Float.parseFloat(str);
}
catch(NumberFormatException e) {
return false;
}
return true;
}
这是 logcat 的例外情况:
05-14 08:54:08.127 31142-31142/com.ritchieengineering.yellowjacket E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.ritchieengineering.yellowjacket, PID: 31142
java.lang.NumberFormatException: Invalid int: "2.5"
at java.lang.Integer.invalidInt(Integer.java:137)
at java.lang.Integer.parse(Integer.java:374)
at java.lang.Integer.parseInt(Integer.java:365)
at java.lang.Integer.parseInt(Integer.java:331)
at java.lang.Integer.valueOf(Integer.java:489)
at com.ritchieengineering.yellowjacket.fragment.TargetSubcoolingFragment.isValidValue(TargetSubcoolingFragment.java:164)
at com.ritchieengineering.yellowjacket.fragment.TargetSubcoolingFragment.onOptionsItemSelected(TargetSubcoolingFragment.java:89)
at android.app.Fragment.performOptionsItemSelected(Fragment.java:1806)
at android.app.FragmentManagerImpl.dispatchOptionsItemSelected(FragmentManager.java:1978)
at android.app.Activity.onMenuItemSelected(Activity.java:2711)
at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1094)
at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:645)
at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:210)
at android.view.View.performClick(View.java:4637)
at android.view.View$PerformClick.run(View.java:19422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5586)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
输入56.3或56.345时如何避免抛出这个异常?我如何检查以确保人们只输入带一位小数的值,例如 56.3?任何帮助将不胜感激。
听起来您的本地文化设置为使用 ,
作为小数点分隔符而不是 .
。您可以使用 NumberFormat
明确设置您的本地文化:
NumberFormat format = NumberFormat.getInstance(Locale.FRENCH);
Number number = format.parse("1,234");
float f = number.floatValue();