How to convert String to int in Java?

In this post, we will be looking at different ways of converting String to int in Java. In most of the cases, we will convert the string Object into the Integer wrapper class object but because of the unboxing feature of Java, that Integer object will automatically be converted into the int primitive data type value.

Let’s look at the above-mentioned ways

Using Integer.parseInt() method

Method declaration – public static int parseInt(String s).
What does it do? It will try to convert the String passed into the function to an Integer, and if it fails to do so because of an invalid String, then it will throw a NumberFormatException.

public class Codekru {

	public static void main(String[] args) {
		String str = "100";
		int i = Integer.parseInt(str);
		System.out.println(i);

	}
}

Output –

100

What if we try to pass an invalid number or not a number at all into the function?

public class Codekru {

	public static void main(String[] args) {
		String str = "100asdf";
		int i = Integer.parseInt(str);
		System.out.println(i);

	}
}

Output –

Exception in thread "main" java.lang.NumberFormatException: For input string: "100asdf"

See, it throws an error, so we don’t recommend doing it 😛

What if there is a negative number?

No issues, negative numbers can also be converted in the same way as positive numbers.

public class Codekru {

	public static void main(String[] args) {
		String str = "-100";
		int i = Integer.parseInt(str);
		System.out.println(i);
		
	}
}

Output –

-100

Using Integer.valueOf() method

The integer class got one more static method to convert String to an int.

Method declarationpublic static Integer valueOf(String s).
What does it do? It also converts an int to String and also throws NumberFormatException on receiving an invalid String in the arguments just like the parseInt() method.

public class Codekru {

	public static void main(String[] args) {
		String str = "100";
		int i = Integer.valueOf(str);
		System.out.println(i);

	}
}

Output –

100

Using Integer() constructor

We can directly convert a String to an integer by just creating a new Integer object and pass the String as the constructor argument, though we don’t recommend using it because it is deprecated according to the java documentation

Deprecated. It is rarely appropriate to use this constructor. Use parseInt(String) to convert a string to a int primitive, or use valueOf(String) to convert a string to an Integer object.

public class Codekru {

	public static void main(String[] args) {
		String str = "-100";
		int i = new Integer(str);
		System.out.println(i);
		
	}
}

Output –

-100

Using DecimalFormat

We can use the DecimalFormat class also to convert a String into an integer. It has the added advantage of converting various kinds of number formats into the integer value ( like converting “123,456,789” into an int).

To achieve our goal here, we would be using the getNumberInstance() static method as shown in the below program.

public class Codekru {

	public static void main(String[] args) {

		try {
			String str1 = "123,456,789";
			String str2 = "-23,456,789";
			String str3 = "3456789";

			int i1 = DecimalFormat.getNumberInstance().parse(str1).intValue();
			int i2 = DecimalFormat.getNumberInstance().parse(str2).intValue();
			int i3 = DecimalFormat.getNumberInstance().parse(str3).intValue();

			System.out.println("i1 value is: " + i1);
			System.out.println("i2 value is: " + i2);
			System.out.println("i3 value is: " + i3);
		} catch (ParseException e) {
			e.printStackTrace();
		}

	}
}

Output –

i1 value is: 123456789
i2 value is: -23456789
i3 value is: 3456789

If you want to know about various other Java conversions, then we suggest you to visit this link.

Referencehttps://docs.oracle.com/javase/9/docs/api/java/lang/Integer.html#Integer-int-

Hope you have liked the article. Please feel free to write us in comments or mail us at [email protected]

Liked the article? Share this on

Leave a Comment

Your email address will not be published.