askIT

Android Convert string to int

If you have a string containing an integer then you can convert it into an int. This can be useful if you are retrieving information from a database or get input data from a text field.

String to int

String myIntString = "5";
int myInt = Integer.parseInt(myIntString);

Example of retrieving text from a text field:

EditText myText = (EditText) findViewById(R.id.entry);
String myString = myText.getText().toString();

int myInt = 0;
try {
        myInt = Integer.parseInt(myString);
}
catch(NumberFormatException nfe) {
        System.out.println("Could not parse " + nfe);
}


Add comment

avatar_blank_60.png

Comment:

Name:

Email:

Comments

No comments yet. You can be the first one to comment. Just write your comment in the form and click on the submit button.