To convert a String to an int in Java, you can use the Integer.parseInt() method. In Java, we can use the Integer.valueOf() method to convert a String to an Integer. We can use the Integer.decode() method to convert a String containing any decimal, such as hexadecimal or octal, into an instance of the Integer class.

The methods parseInt(), valueOf() and decode() are static methods in the Integer class. These methods can be used without creating an instance object of the Integer class. Integer class is a wrapper class for the int data type.

In most cases, we get the data in string format. On a string containing a number, we may need to execute mathematical operations. We must convert the string to an int if the incoming data is in numerical format.



1. Integer.parseInt() method

To convert a string to an int value in Java, use the Integer.parseInt() method. The parseInt() method is a static method that can be used without having to create an Integer object beforehand. The parseInt() method accepts a string containing a number as an input, which will be converted to an int primitive data type value. If the string does not contain a number, the parseInt() method will throw a NumberFormatException. When using the Integer.parseInt() method, the NumberFormatException should be handled in the code.

package com.yawintutor;

public class StringToInt {
	public static void main(String[] args) {
		String str = "12345";
		
		int value = 0;
		try {
			value = Integer.parseInt(str); 
		} catch(Exception e) {}
		System.out.println(value);

		//binary number
		int value1 = 0;
		try {
			value1 = Integer.parseInt("11",2); 
		} catch(Exception e) {}
		System.out.println(value1);
		
		//octal number
		int value2 = 0;
		try {
			value2 = Integer.parseInt("11",8); 
		} catch(Exception e) {}
		System.out.println(value2);
		
		//hexadecimal number
		int value3 = 0;
		try {
			value3 = Integer.parseInt("a",16); 
		} catch(Exception e) {}
		System.out.println(value3);
	}
}

Output

12345
3
9
10


2. Integer.valueOf() method

In Java, use the Integer.valueOf() method to convert a string to an integer value. The valueOf() method is a static method that can be used without having to create an Integer object beforehand. The valueOf() method accepts a string containing a number as an input, which will be converted to an Integer Object. The method valueOf() method will throw a NumberFormatException if the string does not include a number. The NumberFormatException should be handled in the code when using the Integer.valueOf method.

package com.yawintutor;

public class StringToInt {
	public static void main(String[] args) {
		String str = "12345";
		
		int value = 0;
		try {
			value = Integer.valueOf(str); 
		} catch(Exception e) {}
		System.out.println(value1);
	}
}

Output

12345


3. Integer.decode() method

In Java, use the Integer.decode() method to convert a string containing any number such as hexadecimal, octal number to an integer value. The decode() method is a static method that can be used without having to create an Integer object beforehand. The decode() method accepts a string containing a number of any format as an input, which will be converted to an Integer Object. The method decode() method will throw a NumberFormatException if the string does not include a number. The NumberFormatException should be handled in the code when using the Integer.decode() method.

package com.yawintutor;

public class StringToInt {
	public static void main(String[] args) {
		String str = "12345";

		int value = 0;
		try {
			value = Integer.decode(str); 
		} catch(Exception e) {}
		System.out.println(value);
		
		//hexadecimal number
		int value1 = 0;
		try {
			value1 = Integer.decode("0xa"); 
		} catch(Exception e) {}
		System.out.println(value1);

		//octal number
		int value2 = 0;
		try {
			value2 = Integer.decode("010"); 
		} catch(Exception e) {}
		System.out.println(value2);
	}
}

Output

12345
10
8


4. Optional.of() method

The null object in the string is handled using the Optional.of() method. From Java 8, the Optional.Of method is available. The Optional.of() method is used if the input string value contains an optional numeric value.

package com.yawintutor;

public class StringToInt {
	public static void main(String[] args) {
		String str = "12345";
		
		int value = 0;
		try {
			value = Optional.of(Integer.valueOf(str)).get();
		} catch(Exception e) {}
		System.out.println(value1);
	}
}

Output

12345



Leave a Reply