What converting a string to an integer means
When you write a Java program that reads data from a file, a user's keyboard input, or a web form, that data arrives as text — even if it looks like a number. The text "42" is not the same as the number 42 inside your program. Converting a string to an integer means telling Java to read the text and turn it into a number you can do math with, compare, or store in a database.
Java gives you several ways to do this, each one useful in different situations. The most common method works in almost every case, but you need to know what to do when the text does not actually contain a valid number — because your program will crash if you do not handle that.
Key Takeaways
- Use Integer.parseInt() to convert a string to an integer in most cases — it is the standard method and works with positive and negative numbers.
- Always wrap parseInt() in a try-catch block to handle cases where the text is not a valid number, which prevents your program from crashing.
- Use Integer.valueOf() if you need the result as an Integer object rather than a primitive int, though parseInt() is usually the better choice.
- Check that your string contains only digits (and a minus sign if negative) before converting, or be prepared to catch the exception if it does not.
Using Integer.parseInt() for most conversions
Integer.parseInt() is the method you will use most often. It takes a string and returns a primitive int — a whole number your program can use in calculations. Write it like this:
int number = Integer.parseInt("42");
After this line runs, the variable number holds the integer 42, not the text "42". You can now add it to other numbers, compare it, or use it in any place your code expects a number. The method also handles negative numbers: Integer.parseInt("-15") returns -15.
The catch is that if the string contains anything that is not a digit (or a minus sign at the start), Java throws a NumberFormatException and your program stops. This is why you need error handling.
Handling errors when the string is not a valid number
Wrap parseInt() in a try-catch block. The try block contains the code that might fail, and the catch block runs if it does:
try { int number = Integer.parseInt("42"); System.out.println("The number is " + number); } catch (NumberFormatException e) { System.out.println("That is not a valid number"); }
If the string is "42", the try block runs and prints "The number is 42". If the string is "hello" or "42.5" or "4 2" (with a space), the catch block runs instead and prints "That is not a valid number". Your program keeps running either way.
Inside the catch block, you can set a default value, ask the user to try again, log the error, or skip that piece of data. The point is that your program does not crash.
When to use Integer.valueOf() instead
Integer.valueOf() does almost the same thing as parseInt(), but it returns an Integer object instead of a primitive int. In most cases, this does not matter — Java automatically converts between them. But valueOf() is useful when you are storing the result in a collection like an ArrayList or HashMap, because those containers hold objects, not primitives.
Use valueOf() the same way you use parseInt():
Integer number = Integer.valueOf("42");
It also throws NumberFormatException, so wrap it in try-catch the same way. If you are not sure which one to use, parseInt() is the simpler choice for most situations.
Converting strings with leading or trailing spaces
If your string has spaces at the beginning or end — like " 42 " — parseInt() will fail. Use the trim() method first to remove them:
int number = Integer.parseInt(" 42 ".trim());
The trim() method returns a new string with all leading and trailing spaces removed, so parseInt() sees only "42". This is common when reading data from files or user input, because extra spaces slip in easily.
Do not use trim() to remove spaces in the middle of the string — "4 2" will still fail, and that is correct behavior because it is not a valid number.
Converting strings with different number bases
By default, parseInt() assumes the string is in base 10 (decimal). If you need to read a number in a different base — hexadecimal (base 16), binary (base 2), or octal (base 8) — pass a second argument telling parseInt() which base to use:
int hex = Integer.parseInt("FF", 16); // Returns 255 int binary = Integer.parseInt("1010", 2); // Returns 10 int octal = Integer.parseInt("77", 8); // Returns 63
This is less common in everyday programming, but it comes up when you are working with color codes (hexadecimal), file permissions (octal), or data from hardware or network protocols (binary or hex). Always wrap these in try-catch as well, because an invalid character for that base will throw an exception.
Frequently Asked Questions
What is the difference between parseInt and valueOf?
parseInt() returns a primitive int; valueOf() returns an Integer object. For most code, they work the same way because Java converts between them automatically. Use parseInt() unless you specifically need to store the result in a collection or pass it to a method that expects an object.
Why does my program crash when I convert a string to an integer?
The string probably contains something that is not a digit — a letter, a decimal point, a space in the middle, or an empty string. Wrap parseInt() in try-catch to catch the NumberFormatException and handle it gracefully instead of letting your program stop.
Can I convert a decimal number like "3.14" to an integer?
parseInt() will fail on "3.14" because of the decimal point. If you need to convert a decimal string, use Double.parseDouble() first, then cast it to int: int number = (int) Double.parseDouble("3.14"); This gives you 3, dropping the decimal part.
What is the largest number I can convert to an integer?
Java integers can hold values from -2,147,483,648 to 2,147,483,647. If your string represents a larger number, parseInt() throws a NumberFormatException. Use Long.parseLong() instead for larger numbers.