In Java we have multiple ways to convert String to int. We will see a few of those ways in this post.
1. Convert String to int using Integer.parseInt()
1 2 |
String str1 = "500"; int convertedInt = Integer.parseInt(str1); |
If the String value doesn’t contain all digits OR if the String value has all digits but exceeds the max limit that can be allowed in an Integer, then during converting String to int, a NumberFormatException is thrown. So you may want to handle this exception when converting String to int.
A String can also be converted to int using a radix specified by
public static int parseInt(String s,int radix)
See Examples
1 2 3 4 5 6 |
parseInt("0", 10) returns 0 parseInt("500", 10) returns 500 parseInt("+42", 10) returns 42 parseInt("-0", 10) returns 0 parseInt("-FF", 16) returns -255 parseInt("1100110", 2) returns 102 |
In the examples the 2nd param we are passing is the format of String we have received.
10 – regular
16- Hexadecimal
2- Binary
Complete Example
Lets see an example that will use both the methods to convert string to int.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
public class StringToIntExample { public static void main(String[] args) { // Convert regular String String str1 = "500"; try{ int convertedStr1 = Integer.parseInt(str1); System.out.println("After converting Regular String :: " + convertedStr1); }catch(NumberFormatException nfe){ System.out.println("Invalid String!!!!"); } // Convert Hexadecimal String String str2 = "FFE"; try{ int convertedStr2 = Integer.parseInt(str2,16); System.out.println("After converting Hexadecimal String :: " + convertedStr2); }catch(NumberFormatException nfe){ System.out.println("Invalid String!!!!"); } // Convert Binary String String str3 = "1101001"; try{ int convertedStr3 = Integer.parseInt(str3,2); System.out.println("After converting Hexadecimal String :: " + convertedStr3); }catch(NumberFormatException nfe){ System.out.println("Invalid String!!!!"); } // Give illegar value String str4 = "500xxxx"; try{ int convertedStr4 = Integer.parseInt(str4); System.out.println("After converting String :: " + convertedStr4); }catch(NumberFormatException nfe){ System.out.println("Invalid String !!!!"); } } } |
Output
1 2 3 4 |
After converting Regular String :: 500 After converting Hexadecimal String :: 4094 After converting Hexadecimal String :: 105 Invalid String !!!! |
2. Convert String to int using Integer.valueOf()
Similar to Integer.parseInt(), we can convert String to int using Integer.valueOf().
We can also convert the hexadecimals or binary values using Integer.valueOf(String s,int radix)
We will see a small example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public class StringToIntExample { public static void main(String[] args) { // Convert regular String String str1 = "500"; try{ int convertedStr1 = Integer.valueOf(str1); System.out.println("After converting Regular String :: " + convertedStr1); }catch(NumberFormatException nfe){ System.out.println("Invalid String!!!!"); } } } |
Difference between Integer.parseInt and Integer.valueOf()
Integer.parseInt() returns a primitive int while Integer.valueOf() returns a new Integer() object
Integer.valueOf() internally uses Integer.parseInt(), hence if you want a primitive value you should use Integer.parseInt().
Other methods to Convert String to int
There are many other ways to convert string to int. Few of them are
1 2 |
Integer.getInteger(String) Integer.decode(String) |
References
1. Integer API