How to Convert String to int in Java – The following article will let you know the complete information about string to int java, in case if you have any doubts do let me know.
We have different ways to convert a String to int in Java
- Integer.parseInt()
- Integer.valueOf()
- Integer.parseUnsignedInt()
Our own logic equivalent to – Integer.parseInt()
- Integer.decode()
- Integer() constructor
Etc..
String To Int Java
Integer.parseInt() – How It Works:
Syntax1 – int parseInt(String);
Example:
1 2 3 |
Integer.parseInt(“+234”); will result in 234 Integer.parseInt(“234”); will result in 234 |
It converts the given String into an integer. Suppose we give a String “234” as argument to the method, then it returns the integer 234. As this is a static method, we can invoke this using its class name.
Example:
1 2 3 4 5 6 7 |
int a=25; int b=Integer.parseInt(“234”); int c=a+b; System.out.println(c); //will print 259 |
Algorithm for converting string to int without built-in method:
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 |
static int ourMethod(String s) { int n=0,mul=1,len=s.length(); if(len==0) throw new NumberFormatException(); for(int i=0;i<len;i++) { int d=s.charAt(i); if(i==0) if(d==43) continue; else if(d==45) { mul=-1; continue; } d-=48; if(d<0 || d>9) throw new NumberFormatException(s); n=n*10+d; } n*=mul; return n; } |
Implementation:
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 54 |
class StringToInt { public static void main(String args[]) { int x=StringToInt.ourMethod(args[0]);//args[0] is taking input from command line System.out.println("x="+x); System.out.println("x+1="+(x+1)); } static int ourMethod(String s) { int n=0,mul=1,len=s.length(); if(len==0) { throw new NumberFormatException(); } for(int i=0;i<len;i++) { int d=s.charAt(i); if(i==0) { if(d==43) { continue; } else if(d==45) { mul=-1; continue; } } d-=48; if(d<0 || d>9) { throw new NumberFormatException(s); } n=n*10+d; } n*=mul; return n; } } |
Output:
1 2 3 4 |
javac StringToint.java java StringToint 234 x=234 x+1=235 |
Integer.decode() – How It Works
An empty String or a String that contains non-digits will result in Number Format Exception.
Ex:
“234A”, “34.54”, “smile”, “”, etc.
Syntax2: int parseInt(String,int);
This is similar to normal parseInt(), but it takes an integer as second argument and this argument represents the radix of the content in the input String.
Suppose we want to convert an octal number (which is in String format) into its equivalent decimal integer then the second argument should be 8.
Similarly, if we want a hexa-decimal String to its decimal number then the second argument should be 16.
Eg: Integer.parseInt(“234”,10); will result in 234
===> 2*(10^2)+3*(10^1)+4*(10^0)
===> (2*100)+(3* 10)+(4*1)
===> 200+30+4
===> 234
Implementation:
Example:
1 2 3 4 5 6 7 8 9 |
Integer.parseInt(“234”,8);will result in 156 2*(8^2)+3*(8^1)+4*(8^0) (2*64)+(3* 8)+(4*1) 128+24+4 156 |
Integer.parseInt(“234”,2); will result in NumberFormatException as allowed digits in binary(2) number system are 0 and 1 only.
Integer.parseInt(“789”,8); will result in NumberFormatException as allowed digits in octal(8) number system are from 0 to 7 only
string to int java – Integer.valueOf()
It can also be used similar to Integer.parseInt() to convert a String to int. This method also has an overloaded method to take an int (radix) as second argument where as the first argument is a String.
Integer.parseInt() returns the result as a basic (primitive) int type where as Integer.valueOf() returns the result an Integer object.
Example:
- Integer.valueOf(“234”); will give 234 as Integer object
- Integer.valueOf(“412”); will give 412 as Integer object
- Integer.valueOf(“234”,8); will give 156 as Integer object
- Integer.valueOf(“234”,16); will give 564 as Integer object
Integer.parseUnsignedInt()
This is also similar to Integer.valueOf() and Integer.parseInt() but it can work on positive values only.
Example:
- Integer.parseUnsignedInt(“874”); returns 874
- Integer.parseUnsignedInt(“+874”); returns 874
- Integer.parseUnsignedInt(“-874”); raises NumberFormatException
Algorithm:
Syntax: Integer decode(String);
This method converts the given string into an int and returns an Integer object.
Example:
1 2 3 |
Integer num=Integer.decode(“23”); System.out.println(num+2); // will print 25 |
The decode() method can assume the type (decimal, octal, hexadecimal…) of the value and makes the conversion accordingly. In computer parlance, a number preceded by 0 is treated as octal and a number
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Integer.decode(“234”); results in 234 Integer.decode(“0234”); results in 156 Integer.decode(“0x234”); results in 564 Integer.decode(“+234”); results in 234 Integer.decode(“-234”); results in -234 Integer.decode(“234A”); results in NumberFormatException Integer.decode(“twenty”); results in NumberFormatException Integer.decode(“0xabc”); results in 2748 Integer.decode(“0x123”); results in 2 Integer.decode(“0999”); results in NumberFormatException |
Convert String To int java – Integer()
The class Integer has several constructor and one of them receives a String as argument. By using this, we can convert a String to Integer object. Once we have an integer object, we can convert it to an int (primitive) type.
Example:
1 2 3 4 5 |
String s="234"; Integer num=new Integer(s); System.out.println(num+2); //will print 236 |
If you have any doubts related to Convert Java String To Int do leave a comment here.