18 Aug Java Program to convert string to integer
Let’s say the following is our string and we want to convert it to integer:
1 2 3 |
String s = "10" |
Java has the built-in Integer.parseInt() method to convert string to integer. Following is an example:
Example 1
Converting string to integer using the parseInt() method in Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class Example { public static void main( String args[] ) { String s = "10"; System.out.println("String = "+s); System.out.println("Integer = "+Integer.parseInt(s)); } } |
Output
1 2 3 4 |
String = 10 Integer = 10 |
Example 2
Converting string to integer using the valueOf() method in Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class Example { public static void main( String args[] ) { String s = "25"; System.out.println("String = "+s); System.out.println("Integer = "+Integer.valueOf(s)); } } |
Output
1 2 3 4 |
String = 25 Integer = 25 |
No Comments