Converting a string to an integer on Android
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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 courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.