How to convert String to int using Java

Posted on

Convert String to int utilizing Java

 

Utilizing Integer.valueOf() in Java

This technique makes use of valueOf() technique to transform string into Integer object which we will use to print on to console.

Supply Code

Output

Enter worth is 99 in type of string which will likely be transformed to int utilizing valueOf() technique.

Integer.parseInt() Examples

This technique makes use of parseInt() technique for changing string into int in java.

Output

Enter worth is 99 once more and this string worth will likely be transformed to int utilizing parseInt() technique.

This technique can convert any string worth which is convertible to int. However what about values which you’ll’t convert to int for instance “12d”,”aa” ? This java technique will by way of exception when it’s not in a position to convert it to int worth.

Output

java.lang.NumberFormatException is thrown as parseInt() just isn’t in a position to convert 99D worth to int. 

Reference 

https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt(java.lang.String)

Supply projectgeek.com