How to convert a Java String to an Int

© Shutterstock / Shahril KHMD
In this tutorial, Allice Watson, explains how a String can be converted into an int data type in Java [examples included].
Java String/int FAQ: How do I convert a String to an int data type in Java?
Answer: Convert a string to an integer with the parseInt method of the Java Integer class. The parseInt method is to convert the String to an int and throws a NumberFormatException if the string cannot be converted to an int type.
Examples
Java String to int conversion
Overlooking the exception it can throw, use this:
int i = Integer.parseInt(myString);
If the String signified by the variable myString is a valid integer like “1”, “200”, and it will be converted to a Java int. If it fails for any reason, the change can throw a NumberFormatException, so the code should be a little longer to account for this.
Complete String to int
Here is the source code for a whole instance program that demonstrates the Java String to int conversion method, control for a possible NumberFormatException:
public class JavaStringToIntExample { public static void main (String[] args) { // String s = "fred"; // use this if you want to test the exception below String s = "100"; try { // the String to int conversion happens here int i = Integer.parseInt(s.trim()); // print out the value after the conversion System.out.println("int i = " + i); } catch (NumberFormatException nfe) { System.out.println("NumberFormatException: " + nfe.getMessage()); } } }
Let’s discuss
As you look into this example, the Integer.parseInt(s.trim()) method is used to change from the string s to the integer i in this line of code:
int i = Integer.parseInt(s.trim());
If the change attempt fails – in case, if you can try to convert the Java String fred to an int — the Integer parseInt process will throw a NumberFormatException, which you must handle in a try/catch block.
In this case, it’s not essential to use the String class trim() method but in a real-world program, you must use it, so that’s why I’ve shown it here.
Since we’re talking about this, here are a few related notes about the String and Integer classes:
- Integer.toString(int i) is used to convert in the further direction, from an int to a Java String.
- If you’re concerned with converting a String to an Integer object, use the valueOf() method of the Integer class instead of the parseInt() method.
- If you need to convert strings to additional Java primitive fields, use methods like Long.parseLong(), and so on.
nice one
Hi article is very nice, according to my knowledge parsing can be done using onther method called Integer.valueOf(), it is also one of the best way to convert string to int and very easily we can do
Hi article is very nice, according to my knowledge parsing can be done using onther method called Integer.valueOf(), it is also one of the best way to convert string to int and very easily we can do
Great help indeed!!! Thanks :)
Awesome, very well written article. Wonderful points you have mentioned here. Please stay us up to date like this. Thanks for sharing.