Matheus Barreto Matheus Barreto - 1 month ago 10
Android Question

How can I a get a int from a EditText? I tried this algoritm below but the Android Studio return a error ""

My Java code and my xml code. Please what I am doing wrong?

// Get a value from a editText
EditText editTextA = (EditText) findViewById(a_edit_text);
String variableA = editTextA.getText().toString(); //this will get a string
int a =Integer.parseInt(variableA); //this will get a no from the string and casting in integer
Log.v("DiagnosticoActivity", "Number a: " + a);

<EditText
android:id="@+id/a_edit_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Insira a"
android:inputType="number"
android:padding="36dp" />

10-10 11:27:34.916 26195-26195/matheussfdb.dptdedoencas E/AndroidRuntime: FATAL EXCEPTION: main
Process: matheussfdb.dptdedoencas, PID: 26195
java.lang.RuntimeException: Unable to start activity ComponentInfo{matheussfdb.dptdedoencas/matheussfdb.dptdedoencas.DiagnosticoActivity}: java.lang.NumberFormatException: Invalid int: ""
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
at android.app.ActivityThread.access$900(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5527)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
Caused by: java.lang.NumberFormatException: Invalid int: ""
at java.lang.Integer.invalidInt(Integer.java:138)
at java.lang.Integer.parseInt(Integer.java:358)
at java.lang.Integer.parseInt(Integer.java:334)
at matheussfdb.dptdedoencas.DiagnosticoActivity.onCreate(DiagnosticoActivity.java:23)
at android.app.Activity.performCreate(Activity.java:6272)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494) 
at android.app.ActivityThread.access$900(ActivityThread.java:157) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5527) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 

Answer
int a = 0;
try {
    a = Integer.parseInt(variableA); // will only work on numeric entries
} catch (NumberFormatException e) {
    // handle incorrect text entry here
}
// a will be 0 if exception occurred

Jens answered it after my comment

Comments