Java Parse String To Integer

The simplest way to parse a string in e.g. Java or Android to an integer is using the parseint

String myString = "42"; int number = Integer.parseInt(myString);

Note that you will need to catch the the exception NumberFormatException in case the string does not hold a number which can be parsed

try {     myNum = Integer.parseInt(myString.getText().toString()); } catch(NumberFormatException nfe) {    System.out.println("Could not parse " + nfe); } 

Take a further look at integer:

http://developer.android.com/reference/java/lang/Integer.html

Posted in Android, Tips and Tricks Tagged with: , , ,