skip to Main Content

So I am writing this simple app to detect if a number is odd or even. I have defined a Button for detecting, a TextView and an EditText. The app is going to get text from the edittext and make it an int and then use the method to detect if it is odd or even. However, the app crashes at start and throws :

java.lang.NumberFormatException: For input string: "(edttext.getText()).toString()"

What should I do to fix it?

Here is my code:

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {


    public TextView text1;
    public EditText edttext;
    public Button btn1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text1= (TextView) findViewById(R.id.txt1);
        edttext= (EditText) findViewById(R.id.edttxt);
        btn1= (Button) findViewById(R.id.btn);

        String num = "(edttext.getText()).toString()";
        int a = Integer.valueOf(num);
       
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                findOddEven(a);
            }
        });
    }


    public void findOddEven(int num1) {
        if (num1%2==0) {

            text1.setText("Your Number Is Even!");

        }else {
            text1.setText("Your Number Is Odd!");
        }



    }

}

4

Answers


  1. Chosen as BEST ANSWER

    I was finally able to fix this issue by using the suggestion from @Timor. I copied everything about num variable and casting stuff inside the button onclick method. Now it works as it should!


  2. Try to change

        String num = "(edttext.getText()).toString()";
    

    To:

        String num = (edttext.getText()).toString();
    

    The error perform since you try to cast the text (edttext.getText()).toString() to a number, and not its value.

    UPDATED

    Moving the following code into the onClick method, to get the data from the edittext while clicking.

    Login or Signup to reply.
  3. import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;
    
    
    public class MainActivity extends AppCompatActivity {
    
    
        public TextView text1;
        public EditText edttext;
        public Button btn1;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            text1= (TextView) findViewById(R.id.txt1);
            edttext= (EditText) findViewById(R.id.edttxt);
            btn1= (Button) findViewById(R.id.btn);
    
            String num = edttext.getText().toString();
            Integer a=null;
            try{
                  a = Integer.valueOf(num);
                  btn1.setOnClickListener(new View.OnClickListener() {
                  @Override
                      public void onClick(View v) {
                          findOddEven(a);
                      }
                  });
               }catch(NumberFormatException e){
                 // write to log
               }
           
           
        }
    
    
        public void findOddEven(int num1) {
            if (num1%2==0) {
    
                text1.setText("Your Number Is Even!");
    
            }else {
                text1.setText("Your Number Is Odd!");
            }
    
    
    
        }
    
    }
    
    Login or Signup to reply.
  4. Like @markspace already said, your problem is on the assignment of num.
    As you can check in the documentation of the String class (here), a String is just an array of char, and when you do this assignment you are creating an array of strings with these characters, like [‘(‘, ‘e’, ‘d’…] or a string with this value "(edttext.getText())".

    In order to solve this problem, you can just remove the double-quotes. Like this:

    public class MainActivity extends AppCompatActivity {

    public TextView text1;
    public EditText edttext;
    public Button btn1;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        text1= (TextView) findViewById(R.id.txt1);
        edttext= (EditText) findViewById(R.id.edttxt);
        btn1= (Button) findViewById(R.id.btn);
    
        String num = edttext.getText().toString();
        int a = Integer.valueOf(num);
       
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
                findOddEven(a);
            }
        });
    }
    
    
    public void findOddEven(int num1) {
        if (num1%2==0) {
    
            text1.setText("Your Number Is Even!");
    
        }else {
            text1.setText("Your Number Is Odd!");
        }
    
    
    
    }
    

    }

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search