Android Getting a int from a edit text?

2

I am trying to get an int from an edit text set to inputType="number" and then out put it ( i will do something with it later) i cant do it, this is the code i have which i have been told to try but it dose not work

Button go;
EditText num;
TextView OP, ST;
String OPS;
int oer;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    initalize();
    go.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            int myNum = 0;

            OPS = num.getText().toString();

            try {
                myNum = Integer.parseInt(OPS);
            } catch(NumberFormatException nfe) {
                System.out.println("Could not parse " + nfe);
            }
            OP.setText(myNum);
        }

After i run it it force closes. This Is the LOG CAT:

09-09 16:53:00.400: W/ResourceType(20145): No package identifier when getting value for resource number 0x00000008
09-09 16:53:00.400: D/AndroidRuntime(20145): Shutting down VM
09-09 16:53:00.400: W/dalvikvm(20145): threadid=1: thread exiting with uncaught exception (group=0x40a411f8)
09-09 16:53:00.561: E/AndroidRuntime(20145): FATAL EXCEPTION: main
09-09 16:53:00.561: E/AndroidRuntime(20145): android.content.res.Resources$NotFoundException: String resource ID #0x8
09-09 16:53:00.561: E/AndroidRuntime(20145):    at android.content.res.Resources.getText(Resources.java:248)
09-09 16:53:00.561: E/AndroidRuntime(20145):    at android.widget.TextView.setText(TextView.java:3473)
09-09 16:53:00.561: E/AndroidRuntime(20145):    at com.Nutty.studios.randnumgen.free.RandomNumberGenActivity$1.onClick(RandomNumberGenActivity.java:42)
09-09 16:53:00.561: E/AndroidRuntime(20145):    at android.view.View.performClick(View.java:3511)
09-09 16:53:00.561: E/AndroidRuntime(20145):    at android.view.View$PerformClick.run(View.java:14105)
09-09 16:53:00.561: E/AndroidRuntime(20145):    at android.os.Handler.handleCallback(Handler.java:605)
09-09 16:53:00.561: E/AndroidRuntime(20145):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-09 16:53:00.561: E/AndroidRuntime(20145):    at android.os.Looper.loop(Looper.java:137)
09-09 16:53:00.561: E/AndroidRuntime(20145):    at android.app.ActivityThread.main(ActivityThread.java:4575)
09-09 16:53:00.561: E/AndroidRuntime(20145):    at java.lang.reflect.Method.invokeNative(Native Method)
09-09 16:53:00.561: E/AndroidRuntime(20145):    at java.lang.reflect.Method.invoke(Method.java:511)
09-09 16:53:00.561: E/AndroidRuntime(20145):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
09-09 16:53:00.561: E/AndroidRuntime(20145):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
09-09 16:53:00.561: E/AndroidRuntime(20145):    at dalvik.system.NativeStart.main(Native Method)

it works fine if i try to out put the string but not as an integer?

java
android
string
android-edittext
int
asked on Stack Overflow Sep 9, 2012 by Tom Hazell • edited Feb 11, 2019 by Ali Khaki

3 Answers

12

1. You will never get an int from EditText, InputType is provided to provide you with the correct softKeyboard...

2. You can convert the text from EditText in to integer using this..

int i = Integer.parseInt(editText.getText().toString());

moreover setText will expect you to provide it with a String.... so you need to do this...

OP.setText(myNum+"");

toString() only works with Object types, and as you have declared myNum as int, which is a primitive type.... you need to use + ""

answered on Stack Overflow Sep 9, 2012 by Kumar Vivek Mitra • edited Sep 9, 2012 by Kumar Vivek Mitra
2

"inputType = number" does not mean that the input in the EditText would be in the form of an integer. It just implies the type of soft keyboard that would be shown to the user while entering the value and also the valid entries in the EditText,i.e. an EditText with inputType=number would take only numbers and nothing else,but these numbers would be in string format [String number = "12345"] and you would need to manually get the integer value from the string.

Convert String input to integer :

int myNum = Integer.valueOf(editText.getText().toString()).intValue();


Also, your output TextView can only have a string parameter in the setText() and you are trying to pass an integer. TextView.setText("STRING") is the format of showing the output in any TextView and if you are trying to display an integer in a TextView, you would need to convert it to a string first.

Convert integer to String output : OP.setText(Integer.toString(myNum);

answered on Stack Overflow Sep 9, 2012 by Swayam • edited Sep 9, 2012 by Swayam
2

OP.setText(myNum) is causing the crash.

This is due to the nature of setText(int value), where it will look up a string resource rather than set the literal value of the integer.

Instead you would need to do:

OP.setText(String.valueOf(myNum))

Everything else is ok.

answered on Stack Overflow Sep 9, 2012 by Cookster

User contributions licensed under CC BY-SA 3.0