Learnitweb

How to convert String to int in Java ?

In some cases there is a need to convert String to Integer. For example, when client is sending String for key’s value where Integer is expected. There are following two ways to do it:

  • Using Integer.parseInt()
  • Using Integer.valueOf()

parseInt()

This is a static method of Integer class. There are two overloaded methods:

  • public static int parseInt(String s) throws NumberFormatException – Parses the string argument as a signed decimal integer. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign ‘-‘ (‘\u002D’) to indicate a negative value or an ASCII plus sign ‘+’ (‘\u002B’) to indicate a positive value. This method throws NumberFormatException if the string does not contain a parsable integer.
  • public static int parseInt(String s, int radix) throws NumberFormatException – Parses the string argument as a signed integer in the radix specified by the second argument. The characters in the string must all be digits of the specified radix. the first character may be an ASCII minus sign ‘-‘ (‘\u002D’) to indicate a negative value or an ASCII plus sign ‘+’ (‘\u002B’) to indicate a positive value.

    An exception of type NumberFormatException is thrown if any of the following situations occurs:
    – The first argument is null or is a string of length zero.
    – The radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX.
    – Any character of the string is not a digit of the specified radix, except that the first character may be a minus sign ‘-‘ (‘\u002D’) or plus sign ‘+’ (‘\u002B’) provided that the string is longer than length 1.
    – The value represented by the string is not a value of type int.

One major difference between two methods is that parseInt() returns primitive int value whereas valueOf() returns Integer object.

public class StringToIntegerExample {

	public static void main(String[] args) {
		int exmpl1 = Integer.parseInt("500");
		int exmpl2 = Integer.parseInt("0", 10);
		int exmpl3 = Integer.parseInt("500", 10);
		int exmpl4 = Integer.parseInt("+500", 10);
		int exmpl5 = Integer.parseInt("-0", 10);
		int exmpl6 = Integer.parseInt("-FF", 16);
		int exmpl7 = Integer.parseInt("1100110", 2);

		System.out.println("exmpl1: " + exmpl1);
		System.out.println("exmpl2: " + exmpl2);
		System.out.println("exmpl3: " + exmpl3);
		System.out.println("exmpl4: " + exmpl4);
		System.out.println("exmpl5: " + exmpl5);
		System.out.println("exmpl6: " + exmpl6);

		// Uncomment following two lines to observe NumberFormatException
		// int exmpl8 = Integer.parseInt("999999999", 10) throws a NumberFormatException
		// int exmpl9 = parseInt("Hello", 10) throws a NumberFormatException

		// Integer.valueOf returns Integer object
		Integer integer = Integer.valueOf("500");
		System.out.println("Integer value: " + integer);
	}

}

Output

exmpl1: 500
exmpl2: 0
exmpl3: 500
exmpl4: 500
exmpl5: 0
exmpl6: -255
Integer value: 500