Converting a string to an integer on Android


How do I convert a string into an integer?

I have a textbox I have the user enter a number into:

EditText et = (EditText) findViewById(;
String hello = et.getText().toString();

And the value is assigned to the string hello.

I want to convert it to a integer so I can get the number they typed; it will be used later on in code.

Is there a way to get the EditText to a integer? That would skip the middle man. If not, string to integer will be just fine.

1/16/2015 8:33:22 AM

Accepted Answer

See the Integer class and the static parseInt() method:


You will need to catch NumberFormatException though in case of problems whilst parsing, so:

int myNum = 0;

try {
    myNum = Integer.parseInt(et.getText().toString());
} catch(NumberFormatException nfe) {
   System.out.println("Could not parse " + nfe);
5/5/2016 2:55:57 PM

int in = Integer.valueOf(et.getText().toString());
int in2 = new Integer(et.getText().toString());

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow