Converting a string to an integer on Android
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
Converting a string to an integer on Android uses standard Java/Kotlin methods since Android runs on the JVM. The primary methods are Integer.parseInt() (returns a primitive int) and Integer.valueOf() (returns an Integer object). Both throw NumberFormatException if the string is not a valid integer, so you must handle this exception when parsing user input from EditText, intent extras, or API responses. In Kotlin, use toIntOrNull() for a null-safe conversion that avoids try/catch blocks entirely.
Java: Integer.parseInt()
Handling NumberFormatException
Java: Integer.valueOf()
Kotlin: toInt() and toIntOrNull()
Common Android Use Cases
From EditText
From Intent Extras
From SharedPreferences
From JSON/API Responses
Other Numeric Conversions
Common Pitfalls
- Not trimming whitespace before parsing:
Integer.parseInt(" 42 ")throwsNumberFormatExceptionbecause of leading/trailing spaces. Always call.trim()on the string before parsing, especially when reading fromEditText. - Forgetting to handle
NumberFormatException: User input is unpredictable. Parsing without try/catch ortoIntOrNull()crashes the app when the user enters non-numeric text. Always validate or catch exceptions on user-provided strings. - Comparing
Integer.valueOf()results with==:Integer.valueOf()caches values from -128 to 127. Beyond that range,==compares object references and returnsfalseeven for equal values. Use.equals()forIntegerobject comparison. - Parsing strings with decimal points as integers:
Integer.parseInt("3.14")throwsNumberFormatException. To handle decimals, parse asDouble.parseDouble()first, then cast tointif truncation is acceptable:(int) Double.parseDouble("3.14"). - Not setting
inputType="number"on EditText: Withoutandroid:inputType="number"in XML, the soft keyboard shows letters, making non-numeric input easy. Setting the input type restricts the keyboard to digits, reducing (but not eliminating) the need for parsing validation.
Summary
- Use
Integer.parseInt()in Java or.toInt()in Kotlin for basic string-to-integer conversion - Use
.toIntOrNull()in Kotlin for null-safe parsing that avoids try/catch blocks - Always handle
NumberFormatExceptionwhen parsing user input or external data - Trim whitespace before parsing and set
inputType="number"onEditTextto reduce invalid input - For intent extras and SharedPreferences, prefer storing integers directly (
putInt) instead of converting from strings
Related reading
- Converting an int to a binary string representation in Java?
- Converting array to list in Java
- Converting ''ArrayList<String> to ''String[]'' in Java
- Converting between java.time.LocalDateTime and java.util.Date
- Converting a Vision VNTextObservation to a String
- Converting input stream into bitmap
- Converting DynamoDB JSON to Standard JSON with Java
- Converting ISO 8601-compliant String to java.util.Date

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.