Search code examples
androidandroid-fragmentsnumber-formattingnumberformatexception

Android - NumberFormatException Issue


I'm having an issue with the following code when entering in a value with a decimal point. It doesn't matter if I enter in 56. or 56.3 or 56.345, it still makes the app crash and throws the NumberFormatException.

Here is the code where the exception is being thrown:

private boolean isNumeric(String str) {
    try{
        Float.parseFloat(str);
    }
    catch(NumberFormatException e) {
        return false;
    }
    return true;
}

Here is the exception from the 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)

What can I do to keep this exception from being thrown when I enter in 56.3 or 56.345? How can I check to make sure that people are only entering in values with one decimal point like 56.3? Any help would be appreciated.


Solution

  • It sounds like your local culture is set to one that uses a , as decimal point separator instead of a .. You can use NumberFormat to explicitly set your local culture:

    NumberFormat format = NumberFormat.getInstance(Locale.FRENCH);
    Number number = format.parse("1,234");
    float f = number.floatValue();