The most direct way to convert a string to an integer in Java
Use Integer.parseInt() when you have a string that contains only digits and want to turn it into an int. This is the standard method most Java code uses. Pass the string as an argument, and it returns an integer value you can use in calculations or comparisons.
Example: int number = Integer.parseInt("42"); converts the string "42" into the integer 42. The string must represent a valid whole number — no decimal points, no letters, no spaces at the beginning or end.
If the string cannot be converted, Integer.parseInt() throws a NumberFormatException. This is Java's way of telling you the string was not a valid number. You can catch this exception to handle the error gracefully instead of letting your program crash.
Key Takeaways
- Integer.parseInt() is the standard method for converting strings to integers and works for any string that contains only digits, an optional leading minus sign, and no spaces.
- Integer.valueOf() does the same conversion but returns an Integer object instead of a primitive int, which matters if you need to store the value in a collection or pass it to a method expecting an object.
- Always wrap parseInt() or valueOf() in a try-catch block when the string comes from user input or external sources, because invalid strings throw NumberFormatException.
- Strings with leading or trailing spaces, decimal points, or non-numeric characters will cause the conversion to fail unless you trim or clean them first.
- For strings in different number bases (like hexadecimal or binary), use Integer.parseInt() with a second argument specifying the radix, such as Integer.parseInt("1010", 2) for binary.
The difference between Integer.parseInt() and Integer.valueOf()
Both methods convert a string to a number, but they return different types. Integer.parseInt() returns a primitive int — a simple number value. Integer.valueOf() returns an Integer object, which is a wrapper around that number.
In most cases, the difference does not matter because Java automatically converts between them. However, if you are storing values in a collection like an ArrayList or HashMap, you need the Integer object, not the primitive. If you are doing math or comparisons, either works fine.
Integer.valueOf() also caches small integers (typically -128 to 127), so repeated conversions of the same small number are slightly faster. For most everyday code, pick Integer.parseInt() because it is simpler and more direct.
Handling strings that are not valid numbers
When Integer.parseInt() encounters a string it cannot convert, it throws a NumberFormatException. This happens with strings like "abc", "12.5", "12 34" (with a space), or an empty string. Your program stops at that line unless you catch the exception.
Wrap the conversion in a try-catch block to handle this safely:
try { int number = Integer.parseInt(userInput); System.out.println("Converted: " + number); } catch (NumberFormatException e) { System.out.println("That is not a valid number."); }
This way, if the conversion fails, your program prints a message and continues instead of crashing. You can also assign a default value or ask the user to try again.
Cleaning strings before conversion
Strings from user input or external sources often have extra spaces or formatting that Integer.parseInt() will reject. Use the trim() method to remove leading and trailing whitespace before converting.
Example: int number = Integer.parseInt(userInput.trim()); removes spaces from both ends of the string, then converts. If your string has spaces in the middle (like "1 000" for one thousand), you need to remove those manually with replace() before parsing.
For strings with currency symbols, commas, or other formatting, strip those characters out first. A string like "$1,500" will fail to parse, but "1500" will work. The exact cleaning depends on what format your input is in.
Converting strings in different number bases
By default, Integer.parseInt() assumes the string is in base 10 (decimal). If you need to convert a hexadecimal string like "FF" or a binary string like "1010", pass a second argument called the radix to specify the base.
Example: int hex = Integer.parseInt("FF", 16); converts the hexadecimal string "FF" to the integer 255. int binary = Integer.parseInt("1010", 2); converts the binary string "1010" to the integer 10.
The radix can be any value from 2 to 36. Base 2 is binary, base 8 is octal, base 10 is decimal, and base 16 is hexadecimal. This is useful when reading configuration files, parsing network data, or working with systems that use non-decimal numbers.
When to use Integer.parseInt() versus other approaches
Integer.parseInt() is the right choice for most conversions. However, if you need to convert a very large number that exceeds the range of a regular int (which goes up to about 2.1 billion), use Long.parseLong() instead to get a long integer.
If you want to convert a string to a number but are not sure whether it is an integer or a decimal, parse it as a Double first with Double.parseDouble(), then cast it to an int if needed. This is slower and less direct, so only use it when you genuinely do not know the format in advance.
For very defensive code where you want to avoid exceptions entirely, some developers use Apache Commons Lang's NumberUtils.toInt(), which returns a default value if the conversion fails instead of throwing an exception. This is optional — the try-catch approach is standard and works everywhere.
Frequently Asked Questions
What happens if I try to convert a string with a decimal point?
Integer.parseInt() will throw a NumberFormatException because integers do not have decimal places. If your string is "12.5", you must either remove the decimal part manually or use Double.parseDouble() instead and then cast to int (which truncates the decimal).
Can I convert a string with a negative sign?
Yes. Integer.parseInt() handles negative numbers correctly. The string "-42" converts to the integer -42 without any extra work. The minus sign must be at the very beginning of the string with no spaces before it.
What is the maximum number I can convert?
A regular int in Java can hold values from -2,147,483,648 to 2,147,483,647. If your string represents a larger number, use Long.parseLong() instead, which supports much larger values. If the string exceeds even that range, use BigInteger.
Do I need to import anything to use Integer.parseInt()?
No. Integer is part of java.lang, which is imported automatically in every Java program. You can use Integer.parseInt() without any import statement.
Why does my code crash when I convert user input?
User input is often invalid or formatted unexpectedly. Always wrap Integer.parseInt() in a try-catch block when the string comes from outside your code. This catches NumberFormatException and lets you handle the error instead of crashing.