skip to Main Content
enterbutton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        int get_form_edittext=Integer.parseInt(edtx.getText().toString());
        if(get_form_edittext==111){
        flag=1;}
        else{
            flag=0;

        }

First I have initialized the flag variable with 0. I want that if the user enters "o" on the edit text and then presses the enter button the value of the flag variable should be change with 1, else the value should stay 0. I have implemented the above logic but on the click of enter the app has crash.

Logcat

Process: com.sumita.tic_tac_toe, PID: 30411
   java.lang.NumberFormatException: For input string: "o"
           at java.lang.Integer.parseInt(Integer.java:608)
              at java.lang.Integer.parseInt(Integer.java:643)
              at com.sumita.tic_tac_toe.MainActivity$2.onClick(MainActivity.java:36)
              at android.view.View.performClick(View.java:6412)
              at android.view.View$PerformClick.run(View.java:25341)
              at android.os.Handler.handleCallback(Handler.java:790)
              at android.os.Handler.dispatchMessage(Handler.java:99)
              at android.os.Looper.loop(Looper.java:214)
              at android.app.ActivityThread.main(ActivityThread.java:6977)
              at java.lang.reflect.Method.invoke(Native Method)
              at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:528)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:910)

2

Answers


  1. You must be declare inputType="number" in your EditText.

    • Your activity_main.xml
    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number" />
    
    • Your MainActivity.java
    button.setOnClickListener(v -> {
        String editTextString = editText.getText().toString().trim();
        if (editTextString != null && !editTextString.isEmpty()) {
            int value = Integer.parseInt(editTextString);
            flag = value == 111 ? 1 : 0;
        }
    });
    
    Login or Signup to reply.
  2. bro that’s because ‘o’ is not an Integer. the logcat error says it has failed on converting "o" to Integer.
    do it like this
    SOLUTION 1:

      String text = editText.getText().toString();
        if (text != null && !text.isEmpty()) {
           if(string.matches("\d+(?:\.\d+)?")){
             // here the string is a number
             // you can now check for 111 here
           }else{
             // and here the string is not a number 
           }
        }
    

    SOLUTION 2:

    use a try-catch:

       try{
        int get_form_edittext=Integer.parseInt(edtx.getText().toString());
         if(get_form_edittext==111){
           flag=1;
         }else{
           flag=0;
         }
        }catch(Exception e){
           flag=0;
        }
    

    SOLUTION 3:
    make your editText numerical:
    add this to your edit text in your XML

    android:inputType="number"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search