Search code examples
androidvalidationuser-inputis-emptyrange-checking

Check if an Edit Text that only accepts number is not empty and the number is equal or less than 100


I'm building an application for receiving grades and I want to make sure that the Edit Texts are not empty and the values are less or equal to 100 I wrote this line but it crashes the application

if(Integer.parseInt(editText.gettext().toString()) > 100 || editText.getText().toString().trim().length() == 0)
{
//Error message for example
} 

and this is the logCat

09-04 18:21:06.331 8649-8649/com.example.nima.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.example.nima.myapplication, PID: 8649 java.lang.IllegalStateException: Could not execute method of the activity at android.view.View$1.onClick(View.java:3827) at android.view.View.performClick(View.java:4442) at android.view.View$PerformClick.run(View.java:18473) 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:5103) 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:790) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at android.view.View$1.onClick(View.java:3822)             at android.view.View.performClick(View.java:4442)             at android.view.View$PerformClick.run(View.java:18473)             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:5103)             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:790)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)             at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NumberFormatException: Invalid int: "" at java.lang.Integer.invalidInt(Integer.java:137) at java.lang.Integer.parseInt(Integer.java:358) at java.lang.Integer.parseInt(Integer.java:331) at com.example.nima.myapplication.MainActivity.me(MainActivity.java:22)             at java.lang.reflect.Method.invokeNative(Native Method)             at java.lang.reflect.Method.invoke(Method.java:515)             at android.view.View$1.onClick(View.java:3822)             at android.view.View.performClick(View.java:4442)             at android.view.View$PerformClick.run(View.java:18473)             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:5103)             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:790)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)             at dalvik.system.NativeStart.main(Native Method)


Solution

  • In your xml where you have declared edittext make sure that you put the following attribute in edittext element

    android:inputType="number"
    

    And change above code to this :

     if(editText.getText().toString().trim().isEmpty() || Integer.parseInt(editText.gettext().toString()) > 100 )
        {
        //Error message for example
        } 
    

    you first need to check if text is not empty