Java Integer parseInt()
method returns int value after parsing the given string using the specified radix. If radix is not provided, then it uses 10 as radix.
Syntax of parseInt() method
public static int parseInt(String s) throws NumberFormatException public static int parseInt(String s, int radix) throws NumberFormatException //this overloaded version is introduced in java 1.9 public static int parseInt (CharSequence seq, int beginIndex, int endIndex, int radix)
parseInt() Parameters
s
– String to be parsed.radix
– radix that is used for parsing.beginIndex
– The beginning index in the given character sequence. This is inclusive.endIndex
– The ending index in the given char sequence. This is exclusive.
parseInt() Return Value
- An int value represented by the argument.
- It throws
NumberFormatException
, if the given string or char sequence is not parsable. - It throws
NullPointerException
, if the given char sequence is null. - It throws
IndexOutOfBoundsException
, ifbeginIndex
is negative,beginIndex > endIndex
orendIndex > s.length()
.
Example 1: Integer.parseInt(String s)
In this example, the radix is not specified while calling parseInt()
method so it parsed the strings using default radix 10 (10 is for decimal).
public class JavaExample { public static void main(String[] args) { String s1 = "100"; String s2 = "+50"; String s3 = "-50"; int i1 = Integer.parseInt(s1); int i2 = Integer.parseInt(s2); int i3 = Integer.parseInt(s3); System.out.println("No sign int: "+i1); //default +ve System.out.println("Positive int: "+i2); System.out.println("Negative int: "+i3); } }
Output:
Example 2: Integer.parseInt(String s, int radix)
By specifying radix, we can parse a string that contains hex digits, octal digits etc. In the following example, we have a string that contains hex value, we can parse this hex value to int using radix 16.
public class JavaExample { public static void main(String[] args) { String s1 = "100"; String s2 = "EF"; //hex value String s3 = "77"; //decimal: 77, octal: 63 int i1 = Integer.parseInt(s1, 10); //radix 10 for decimal int i2 = Integer.parseInt(s2, 16); //radix 16 for hex int i3 = Integer.parseInt(s3, 8); //radix 8 for octal System.out.println("int value: "+i1); System.out.println("int value: "+i2); System.out.println("int value: "+i3); } }
Output:
Some more examples of parsing with radix:
//returns 0 parseInt("0", 10) //returns 115 parseInt("115", 10) //returns 115 parseInt("+115", 10) //returns -115 parseInt("-115", 10) //returns -238 parseInt("-EE", 16) //returns 93 parseInt("1011101", 2) //returns 1001 parseInt("1001", 10) //returns -1001 parseInt("-1001", 10) //this is not a valid octal number parseInt("99", 8) throws a NumberFormatException //this is not a valid number parseInt("hello", 10) throws a NumberFormatException
Example 3: Integer parseInt with CharSequence and indexes
Here, we have two char sequences and we are parsing the part of the these sequences using parseInt()
method of Integer class.
public class JavaExample { public static void main(String[] args) { String s = "Hello1001Bye"; String s2 = "LEAF"; //taking 1001 part from string s int i = Integer.parseInt(s, 5, 9, 10); //taking EA from the string s2 and using 16 to parse it as hex int i2 = Integer.parseInt(s2, 1, 3, 16); System.out.println("int value: "+i); //Hex "EA" is equal to 234 in decimal System.out.println("int value: "+i2); } }
Output: