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)
If you want to control what digit should enter use
android:digits=""
android:inputType="number"
enforce to open number type keyboard still user can press other char like "#,." than may cause NumberFormatExceptionandroid:digits="0123456789" only accepts 0123456789 in
EditText
, not any other charIn your xml where you have declared edittext make sure that you put the following attribute in edittext element
And change above code to this :
you first need to check if text is not empty
You only have to switch the 2nd ans 1st expressions of the if. Because when the program runs it checks firstly the 1st expression and then the 2nd.
So... when you don't have any value on the editText the getText().toString() return this "" and its impossible to parse to Int that value so you have to check first the length() and after that check if the number is bigger than 100
Your logcat states that you are entering a String where the code requires a Integer.
What i would suggest is store the EditText value in a different String and then check typecast it into Integer, many a times the compiler gets confused there.
Hope this helps :)
First You Put In Your XML EditText Input Type=Number
Then Write This Code