Convert String to Integer – parseInt()




Convert String to Integer – parseInt()

parseInt() method can be used to convert a String representation of an Integer to int.

There are two variants of parseInt() method in Java.

(1) int java.lang.Integer.parseInt(String s) throws NumberFormatException
(2) int java.lang.Integer.parseInt(String s, int radix) throws NumberFormatException

Let’s discuss each of these methods in detail.

int java.lang.Integer.parseInt(String s) throws NumberFormatException

It Parses the string argument as a signed decimal integer. The default radix of this method is 10.

Signed decimal integer means that sign of the string representation of number will be maintained. i.e. If the number in the String is represented as “-73832” , then after parsing it will negative integer only.

NumberFormatException is not a checked exception and hence, we don’t need to catch or throws to compile the code.

package com.masterjavatutorial;

public class StringToIntegerDemo {

	public static void main(String[] args) {
		 String str = "-7426";
		 int number = Integer.parseInt(str);
		 System.out.println("Number = "+number); 
		 
	}

}

The output of the above program is as below:
Number = -7426

Note: If the str = “+7426” , then it would give the output as Number = 7426

What if the String contains alphanumeric or characters only

if the String contains alphanumeric or characters only , then it would throw NumberFormatException as explained below by the demo program.

package com.masterjavatutorial;

public class StringToIntegerDemo {

	public static void main(String[] args) {
		 String str = "7hello426";
		 int number = Integer.parseInt(str);
		 System.out.println("Number = "+number); 
		 
	}

}

The output of the above program is as below:

Exception in thread "main" java.lang.NumberFormatException: For input string: "7hello426"
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.lang.Integer.parseInt(Integer.java:580)
	at java.lang.Integer.parseInt(Integer.java:615)
	at com.masterjavatutorial.StringToIntegerDemo.main(StringToIntegerDemo.java:7)

What does radix mean??

By defualt, the radix is taken as 10. This is used while parsing the string to int.

e.g. “7426” is parsed with radix 10 as

7*(10)^3+4*(10)^2 + 2*(10)^1+6*(10)^0
=7000+400+20+6 = 7426

What if fractional numbers are parsed as int??

e.g.
String str = “3426.123”;
int number = Integer.parseInt(str);

Then , this will give NumberFormatException.

int java.lang.Integer.parseInt(String s,int radix) throws NumberFormatException

This method differs in many ways from parseInt(String s) method above used for parsing.

It parses according to the radix which is passed as an argument where radix can range from 2 to 36 inclding both.

Character.MIN_RADIX = 2
Character.MAX_RADIX = 36

The below demo program explains how it is parsed basis radix.

package com.masterjavatutorial;
public class StringToIntegerDemo {

	public static void main(String[] args) {
	    String str = "2546";
	    int number = Integer.parseInt(str,8);
	    System.out.println("Number = "+number); 
	}

}

The output of the above program is as below:
Number = 1382

2546 is parsed on radix 8 as : 2*(8)^3+5*(8)^2+4*(8)^1+6*(8)^0 = 2*512+5*64+4*8+6 = 1024+320+32+6 = 1382

Can str in this method while parsing using radix contain characters???

Yes, it is possible that the str can contain characters. It would depend on the radix whether exception will come or not.

e.g. character a to z are used to represent digits from 10 to 35.

so, If you use radix as 10, you can not use characters in string while parsing.
If you use radix as 11, you can use only ‘a’ character in string to represent digit 10 while parsing.
If you use radix as 12, you can use only ‘a’,’b’ character in string to represent digit 10 and 11 while parsing.
and so on…
If you use radix as 36, you can use a to z character in string to represent digit from 10 to 36 while parsing.

package com.masterjavatutorial;

public class StringToIntegerDemo {

	public static void main(String[] args) {
		 String str = "5afz46";
		 int number = Integer.parseInt(str,36);
		 System.out.println("Number = "+number); 
		 
	}

}

The output of the above document is as below :
Number = 319872390

NOTE : While parsing if the int goes beyond the range of int , then it would give you NumberFormatException.






Leave a Reply