How can I convert String to Int?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Java, converting a String to an int is usually done with Integer.parseInt. That sounds simple, but real code also has to deal with invalid input, whitespace, and numbers that may not fit inside the int range. A good solution therefore combines parsing with validation and clear error handling.
The Standard Java Conversion
The most direct conversion uses Integer.parseInt.
This works when the string is a valid base-10 integer inside the int range.
If the string is not valid, Java throws NumberFormatException.
Handle Invalid Input Safely
In production code, you usually should not assume the text is always valid.
This is the standard pattern when the input comes from a user, a file, or an API.
Trim Whitespace Before Parsing
Input often contains spaces around the number. parseInt does not automatically accept arbitrary surrounding whitespace in the way many developers expect, so trimming is a good habit.
If you skip trim(), seemingly valid input may fail.
parseInt Versus valueOf
Java also provides Integer.valueOf.
The main difference is that parseInt returns a primitive int, while valueOf returns an Integer object. If you only need a primitive, parseInt is the clearer choice.
Other Bases And Numeric Formats
If the string is in another base, pass the radix explicitly.
This prints 10 because the input is interpreted as binary.
That is useful when parsing hexadecimal, binary, or other fixed-base input formats.
Build A Helper When Parsing Happens Often
If your code parses user input frequently, a helper method can make the call sites cleaner.
Java does not have a built-in TryParse method like C#, so helpers like this are common when exceptions are not convenient at the call site.
Common Pitfalls
The most common mistake is assuming the string is always valid. Any non-numeric character, bad sign placement, or overflow will trigger NumberFormatException.
Another issue is forgetting to trim whitespace before parsing user input.
It is also easy to parse into the wrong numeric type. If the number may exceed int, use Long.parseLong or new BigInteger(...) instead.
Finally, do not confuse character codes with numeric parsing. Converting a string digit such as "7" to 7 is not the same as reading the Unicode value of the character.
Summary
- Use
Integer.parseIntfor normal Java string-to-intconversion. - Wrap parsing in
try/catchwhen input may be invalid. - Trim input first if it may contain surrounding whitespace.
- Use
valueOfonly when you specifically want anIntegerobject. - Choose a larger numeric type if the input may exceed the
intrange.
Related reading
- How can I create a generic array in Java?
- How can I create a Java 8 LocalDate from a long Epoch time in Milliseconds?
- How can I create an Array of ArrayLists?
- How can I create many kafka topics during spring-boot application start up?
- How can I create multiple servers with Java RMI?
- How can I determine what core a Java thread is running on?
- How can I determine whether a Java class is abstract by reflection
- How can I disable spring cloud stream for development purpose when there are not kafka broker running?

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.